解决线程无法析构的问题

TaoZhang-Branch
wuyize 2023-03-09 21:39:32 +08:00
parent 52734adda8
commit f504480050
5 changed files with 46 additions and 56 deletions

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"> <ui version="4.0">
<class>MainWindowClass</class> <class>MainWindowClass</class>
<widget class="QMainWindow" name="MainWindowClass"> <widget class="QWidget" name="MainWindowClass">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>0</x> <x>0</x>
@ -10,19 +10,9 @@
<height>722</height> <height>722</height>
</rect> </rect>
</property> </property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<property name="styleSheet"> <property name="styleSheet">
<string notr="true">font: 10pt &quot;Segoe UI, Microsoft YaHei UI&quot;;</string> <string notr="true">font: 10pt &quot;Segoe UI, Microsoft YaHei UI&quot;;</string>
</property> </property>
<widget class="QWidget" name="centralWidget">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing"> <property name="spacing">
<number>0</number> <number>0</number>
@ -60,7 +50,6 @@
</item> </item>
</layout> </layout>
</widget> </widget>
</widget>
<layoutdefault spacing="6" margin="11"/> <layoutdefault spacing="6" margin="11"/>
<customwidgets> <customwidgets>
<customwidget> <customwidget>

View File

@ -12,14 +12,13 @@
FRAMELESSHELPER_USE_NAMESPACE FRAMELESSHELPER_USE_NAMESPACE
CentralWidget::CentralWidget(QWidget* parent) : QMainWindow(parent) CentralWidget::CentralWidget(QWidget* parent) : QWidget(parent)
{ {
ui.setupUi(this); ui.setupUi(this);
NavigationBarWidget* navigationBarWidget = new NavigationBarWidget(this); NavigationBarWidget* navigationBarWidget = new NavigationBarWidget(this);
ui.gridLayout->addWidget(navigationBarWidget, 0, 0, 1, 1, Qt::AlignTop | Qt::AlignHCenter); ui.gridLayout->addWidget(navigationBarWidget, 0, 0, 1, 1, Qt::AlignTop | Qt::AlignHCenter);
QObject::connect(navigationBarWidget->tabs, &QtMaterialTabs::currentChanged, ui.stackedWidget, &QStackedWidget::setCurrentIndex); QObject::connect(navigationBarWidget->tabs, &QtMaterialTabs::currentChanged, ui.stackedWidget, &QStackedWidget::setCurrentIndex);
QObject::connect(ui.stackedWidget, &QStackedWidget::currentChanged, QObject::connect(ui.stackedWidget, &QStackedWidget::currentChanged, ui.rendererWidget, &Renderer::RendererWidget::currentTabChanged);
ui.rendererWidget, &Renderer::RendererWidget::currentTabChanged);
} }
CentralWidget::~CentralWidget() CentralWidget::~CentralWidget()
@ -40,7 +39,7 @@ MainWindow::MainWindow(QWidget* parent, const Qt::WindowFlags flags)
m_titleBar->chromePalette()->setTitleBarActiveBackgroundColor(QColor(0, 0, 0, 0)); m_titleBar->chromePalette()->setTitleBarActiveBackgroundColor(QColor(0, 0, 0, 0));
m_titleBar->chromePalette()->setTitleBarInactiveBackgroundColor(QColor(0, 0, 0, 0)); m_titleBar->chromePalette()->setTitleBarInactiveBackgroundColor(QColor(0, 0, 0, 0));
m_central_widget = new CentralWidget(); m_central_widget = new CentralWidget(this);
setMenuWidget(m_titleBar); setMenuWidget(m_titleBar);
setCentralWidget(m_central_widget); setCentralWidget(m_central_widget);
@ -73,4 +72,5 @@ void MainWindow::closeEvent(QCloseEvent* event)
if (result != QMessageBox::Yes) if (result != QMessageBox::Yes)
event->ignore();*/ event->ignore();*/
qDebug() << "closeEvent";
} }

View File

@ -9,7 +9,7 @@ FRAMELESSHELPER_BEGIN_NAMESPACE
class StandardTitleBar; class StandardTitleBar;
FRAMELESSHELPER_END_NAMESPACE FRAMELESSHELPER_END_NAMESPACE
class CentralWidget : public QMainWindow class CentralWidget : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: public:

View File

@ -117,7 +117,7 @@ QRectF calcBoundingRect(const QPainterPath& path, const std::vector<BaseStyle>&
Renderer::ElementRenderer::ElementRenderer() Renderer::ElementRenderer::ElementRenderer()
{ {
surface.create(); surface.create();
thread = std::jthread([&] { thread = std::jthread([&](std::stop_token stop) {
QOpenGLContext context; QOpenGLContext context;
context.create(); context.create();
context.makeCurrent(&surface); context.makeCurrent(&surface);
@ -131,7 +131,7 @@ Renderer::ElementRenderer::ElementRenderer()
initialized = true; initialized = true;
while (true) while (!stop.stop_requested())
{ {
std::unique_lock<std::mutex> lock(drawMutex); std::unique_lock<std::mutex> lock(drawMutex);
draw.wait(lock, [&] {return needDraw; }); draw.wait(lock, [&] {return needDraw; });

View File

@ -18,7 +18,7 @@ Renderer::VirtualTextureManager::VirtualTextureManager(GladGLContext* glMain)
surface.create(); surface.create();
mainContext->doneCurrent(); mainContext->doneCurrent();
thread = std::jthread([&] { thread = std::jthread([&](std::stop_token stop) {
QOpenGLContext context; QOpenGLContext context;
context.setFormat(mainContext->format()); context.setFormat(mainContext->format());
context.setShareContext(mainContext); context.setShareContext(mainContext);
@ -67,7 +67,7 @@ Renderer::VirtualTextureManager::VirtualTextureManager(GladGLContext* glMain)
GLuint pageLoadTimeQuery; GLuint pageLoadTimeQuery;
gl->GenQueries(1, &pageLoadTimeQuery); gl->GenQueries(1, &pageLoadTimeQuery);
while (true) while (!stop.stop_requested())
{ {
if (needUpdate) if (needUpdate)
{ {
@ -85,6 +85,7 @@ Renderer::VirtualTextureManager::VirtualTextureManager(GladGLContext* glMain)
//qDebug() << duration; //qDebug() << duration;
pageLoadDuration += duration; pageLoadDuration += duration;
} }
std::this_thread::yield();
} }
}); });