Qt QSplitter and unresponsive GUI (cpu 100%)

☆樱花仙子☆ 提交于 2019-12-21 06:49:13

问题


I'm trying to implement the following layout

|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
|               |                |
|               |                |
|   QTABWIDGET  |  QGLWIDGET     |
|               |                |
|               |                |
|_______________|________________|
|                                |
|                                |
|          TEXTEDIT              |
|________________________________|

Between the TabWidget and GLWidget the layout is governed by a QSplitter with horizontal orientation. Another QSplitter with vertical orientation is needed between the previous splitter and QTextEdit widget so that I can choose to hide the textedit.

Currently my implementation is the following (this is the pointer to MainWindow class):

QVBoxLayout *mainWindowLayout = new QVBoxLayout(ui->centralWidget);
// Here we setup an horizontal splitter between the TabWidget and the QGLWidget
QSplitter *glTabSplitterHorizontal = new QSplitter(Qt::Horizontal,this);
glTabSplitterHorizontal->addWidget(ui->tabWidget); // seems to produce the high CPU load
glTabSplitterHorizontal->addWidget(this->glWidget);

// add the horizontal splitter as first row of the layout
QSplitter *splitterConsoleVertical = new QSplitter(Qt::Vertical,this);
splitterConsoleVertical->setOrientation(Qt::Vertical);
// as first row it must be the previously allocated horizontal layout tabWidget
splitterConsoleVertical->addWidget(glTabSplitterHorizontal);
mainWindowLayout->addWidget(glTabSplitterHorizontal);

My application seems to work correctly, but when I maximize it, the CPU load jumps to 90% and above and the gui interface is slow!

I've found that you can't put a layout inside a QSplitter http://qt-project.org/doc/qt-4.8/qsplitter.html

so I've tried to comment the line glTabSplitterHorizontal->addWidget(ui->tabWidget); and the CPU is not heavy loaded. The problem is that I need that tabWidget!

How can I work around this issue, keeping my layout with splitters?


回答1:


I've restructured my code in the following way:

QSplitter *splitHorizontal = new QSplitter;
QSplitter *splitVertical = new QSplitter;
QVBoxLayout *layout = new QVBoxLayout;
QWidget *container = new QWidget;
QVBoxLayout *container_layout = new QVBoxLayout;
splitHorizontal->addWidget(ui->tabWidget);
splitHorizontal->addWidget(this->glWidget);
container_layout->addWidget(splitHorizontal);
container->setLayout(container_layout);
splitVertical->setOrientation(Qt::Vertical);
splitVertical->addWidget(container);
splitVertical->addWidget(new QTextEdit());
layout->addWidget(splitVertical);
this->centralWidget()->setLayout(layout);
this->centralWidget()->show();

following the suggestion in this answer

Qt - Making a Splitter Horizontal and Vertical at same time

and the CPU now is no more heavy loaded.



来源:https://stackoverflow.com/questions/13868495/qt-qsplitter-and-unresponsive-gui-cpu-100

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!