0

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:

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*)));
}

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();
}
Dale Wilson
  • 9,166
  • 3
  • 34
  • 52
Sessa93
  • 3
  • 4
  • i think you may need to show some more code. I suspect the scope of your objects in relation to your call to QApplcation::exec() may be the issue – tinkertime Jul 29 '14 at 18:20

1 Answers1

1

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);
}
jpo38
  • 20,821
  • 10
  • 70
  • 151
  • Yes my class specializes QMainWindow, but your suggestion doesn't work, same result as before. – Sessa93 Jul 29 '14 at 18:17
  • In addition to setting a central widget, you also need to set the layout for this new widget instead of the main window itself: `mainWidget->setLayout(mainLayout)`. See also this question: http://stackoverflow.com/questions/25001250/widgets-are-not-shown-in-basic-qt-application-qmainwindow – Akos Bannerth Jul 29 '14 at 18:46
  • Actually, doing `mainLayout = new QHBoxLayout(mainWidget);` does this automatically (layout created is attached to its parent). No need to call `mainWidget->setLayout(mainLayout)` here (at least with recent Qt versions, >4.8,...dunno for old ones). – jpo38 Jul 29 '14 at 19:00
  • Did you add mainWidget + use it as parent for `mainLayout`, `tabber` and `analysisTreeView`?? – jpo38 Jul 29 '14 at 19:01
  • Thanks jpo38 now it works, i didn't notice that i needed to add mainWidget as parent of mainLayout, tabber and analysisTreeView. – Sessa93 Jul 30 '14 at 06:43