小部件不在QT主窗口中显示 [英] Widgets not shown in QT main window
问题描述
我正在构建一个简单的应用程序,主窗口必须显示两个小部件(右侧的QTreeView,QTabWidget左侧),执行这里我使用一个QHBoxLayout。
这是我写的代码(MainWindow的构造函数):
I'm building a simple application, the main window have to shows two widgets (QTreeView on the right, QTabWidget on the left), to perform this i use a QHBoxLayout. This is the code i've written(constructor of MainWindow):
MainWindow::MainWindow()
{
mainLayout = new QHBoxLayout(this);
tabber = new QTabWidget(this);
analysisTreeView = new QTreeView(this);
tabber->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
mainLayout->addWidget(tabber, 0);
mainLayout->addWidget(analysisTreeView, 0);
createActions();
createMenus();
createToolBars();
connect(tabber, SIGNAL(currentChanged(int)), this, SLOT(currentTabChanged(int)));
setLayout(mainLayout);
}
但是当我运行应用程序时,主窗口不显示任何小部件。为什么?
But when i run the application the main window shows no widgets. Why?
根据要求,我添加了一些代码:
On request, i add some code:
一旦主窗口工具栏中的按钮被点击新标签页添加到tabber:
Once a button in the mainwindows's toolbar is clicked a new tab is added to tabber:
void MainWindow::newSheet()
{
GraphicsScene *newScene = new GraphicsScene(itemMenu,this);
QGraphicsView *newView = new QGraphicsView(this);
newScene->setSceneRect(-200, -200, 400, 400);
newView->scale(1.5,1.5);
newView->setCacheMode(QGraphicsView::CacheBackground);
newView->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
newView->setRenderHint(QPainter::Antialiasing);
newView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
newView->setScene(newScene);
sheetList.append(newView);
tabber->addTab(newView,"PNC");
connect(newScene, SIGNAL(itemInserted(PItem*)), this, SLOT(itemInserted(PItem*)));
connect(newScene, SIGNAL(requestUpdateGUI(GraphicsScene*)), this, SLOT(updateGUI(GraphicsScene*)));
}
我的main.cpp:
My main.cpp:
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(application);
QApplication a(argc, argv);
MainWindow window;
window.showMaximized();
return a.exec();
}
推荐答案
code> QMainWindow 。如果是,则需要设置一个centralWidget:
I suppose your class specializes QMainWindow
. If so, it needs a centralWidget to be set:
MainWindow::MainWindow()
{
// added by jpo38
QWidget* mainWidget = new QWidget( this );
setCentralWidget( mainWidget );
// end added by jpo38
mainLayout = new QHBoxLayout(mainWidget);
tabber = new QTabWidget(mainWidget);
analysisTreeView = new QTreeView(mainWidget);
tabber->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
mainLayout->addWidget(tabber, 0);
mainLayout->addWidget(analysisTreeView, 0);
createActions();
createMenus();
createToolBars();
connect(tabber, SIGNAL(currentChanged(int)), this, SLOT(currentTabChanged(int)));
setLayout(mainLayout);
}
这篇关于小部件不在QT主窗口中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!