3

I am trying to create main application window. It is supposed to have a menu bar, and show one label with splitter underneath. My code for main window is below.

The problem I have is that menu bar shows but label and splitter do not. Things I tried so far: 1. Set splitter's parent to "this" -> splitter shows, but is drawn over menu bar and is small. 2. Set label's parent to "this" -> label shows, but is drawn over menu bar. 3. Set parent to "this" for splitter, parent and label ->label doesn't show, I get miniature version of slipper drawn underneath menu bar. 4. Tried reshuffling of code lines and got various other results, like big splitter drawn over menu bar that doesn't resize with window, small splitter underneath menu bar, just menu bar and nothing else, etc -- nothing useful.

Looks like vertical layout is completely ignored.

No idea what else to try. Any suggestions?

MyWindow::MyWindow(IViewSignalHandler* signalHandler, QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
{
    m_signalHandler = signalHandler;

    // menu
    m_fileMenu = new QMenu(tr("&File"));
    m_fileMenu->addAction(tr("&Open"), this, SLOT(slot_OpenFile(bool)));

    m_helpMenu = new QMenu(tr("&Help"));
    m_helpMenu->addAction(tr("&About"), this, SLOT(slot_ShowAboutBox(bool)));

    menuBar()->addMenu(m_fileMenu);
    menuBar()->addMenu(m_helpMenu);

    // graph
    m_graphWidget = new QwtPlot();
    m_graphLegend = new QwtLegend();
    m_graphLegend->setItemMode(QwtLegend::CheckableItem);
    m_graphWidget->insertLegend(m_graphLegend, QwtPlot::RightLegend);
    m_graphWidget->setAxisTitle(QwtPlot::xBottom, tr("X"));
    m_graphWidget->setAxisScale(QwtPlot::xBottom, DEFAULT_X_MIN, DEFAULT_X_MAX);
    m_graphWidget->setAxisTitle(QwtPlot::yLeft, tr("Y"));
    m_graphWidget->setAxisScale(QwtPlot::yLeft, DEFAULT_Y_MIN, DEFAULT_Y_MAX);

    QwtPlotZoomer* zoomer = new QwtPlotZoomer(m_graphWidget->canvas());
    zoomer->setTrackerMode(QwtPlotZoomer::AlwaysOn);
    zoomer->setMousePattern(QwtEventPattern::MouseSelect2, Qt::RightButton, Qt::ControlModifier);
    zoomer->setMousePattern(QwtEventPattern::MouseSelect3, Qt::RightButton);

    // path label
    m_label= new QLabel();
    m_label->setTextFormat(Qt::RichText);
    m_label->setWordWrap(false);
    m_label->setText(tr("<b>Label: </b>"));

    // splitter
    m_splitter = new QSplitter();
    m_splitter->setChildrenCollapsible(true);
    m_list = new QListWidget();
    m_splitter->addWidget(m_list);
    m_tree = new QTreeWidget();
    m_splitter->addWidget(m_tree);
    m_text = new QTextEdit();
    m_splitter->addWidget(m_text);
    m_splitter->addWidget(m_graphWidget);

    // page layout
    QVBoxLayout *pageLayout = new QVBoxLayout(this);
    pageLayout->addWidget(m_label);
    pageLayout->addWidget(m_splitter);
    setLayout(pageLayout);
}

[...]

m_mainWindow = new MyWindow(this);
m_mainWindow->show();

2 Answers2

1

I just had the exact same problem. I do not know what caused it, but inheriting from QWidget instead of QMainWindow seems to have fixed it.

ShdNx
  • 3,172
  • 5
  • 40
  • 47
1

You need to set a central widget by calling setCentralWidget().

EDIT: Add a QWidget to your Main Window, set it as central widget, create your layout and finally add it to the central widget.

MyWindow::MyWindow(IViewSignalHandler* signalHandler, QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
  QWidget *ui_area = new QWidget;
  setCentralWidget(ui_area);

  //.....create your_layout.....

 ui_area->setLayout(your_layout);

}
Arnold Spence
  • 21,942
  • 7
  • 74
  • 67
  • I tried that in various combinations but no cookie. For example, if I add `this->setCentralWidget(pageLayout->widget());` as a last line in constructor above I then get mini-splitter drawn over menu bar. – Galadrius Krunthar Mar 23 '12 at 01:32
  • It looks much better if I try `this->setCentralWidget(m_splitter);` -- I then get nice menu bar with large splitter just below that resizes with the window, however this way I use label which is supposed to be between menu bar and splitter (reason why I have vertical layout there). – Galadrius Krunthar Mar 23 '12 at 01:38
  • If I switch from `QMainWindow` to `QDialog` everything works fine. – Galadrius Krunthar Mar 23 '12 at 05:22
  • -1: a QLayout is not a QWidget. setCentralWidget() is not applicable in this case. – ShdNx May 30 '13 at 11:42