Title Bar is Pushed to the Right in QT Dock Widget
-
Hello everyone, hope all is well! Currently, I am trying to host a QChart inside a dockWidget, and here is the code I am using (huge thanks to those who helped me out the other day) in my MainWindow file thus far:
[MainWindow.cpp]
QFrame *frame = new QFrame(); QLineSeries *series = new QLineSeries(); series->append(0, 6); series->append(2, 4); series->append(3, 8); series->append(7, 4); series->append(10, 5); *series << QPointF(11, 1) << QPointF(13, 3) << QPointF(17, 6) << QPointF(18, 3) << QPointF(20, 2); QChart *chart = new QChart(); chart->legend()->hide(); chart->addSeries(series); chart->setTitle("Line Chart Example"); QChartView *chartView = new QChartView(chart); chartView->setRenderHint(QPainter::Antialiasing); chartView->setMinimumSize(600,600); chartView->setParent(frame); QComboBox *q = new QComboBox(); q->setParent(frame); frame->setParent(ui->dockWidget);
However, I have run into a problem, as the title bar is pushed to the right. Here is a picture of what I am talking about:
I would like to have the chart (and combo box) under the title bar like one would expect. I would especially like the title box to be seen as one would normally expect; currently, the title bar window buttons (close, minimize/fullscreen buttons) are not even in the frame. What would I need to do to fix this issue?
-
You should add your widgets to a layout.
-
Don't assign parents manually. Use layouts instead. Also docks already have an internal layout so use their
setWidget
instead.In short replace this:
QFrame *frame = new QFrame(); QChartView *chartView = new QChartView(chart); chartView->setParent(frame); QComboBox *q = new QComboBox(); q->setParent(frame); frame->setParent(ui->dockWidget);
with:
QFrame *frame = new QFrame(); QChartView *chartView = new QChartView(chart); QComboBox *q = new QComboBox(); QVBoxLayout* layout = new QVBoxLayout(frame); layout->addWidget(q); layout->addWidget(chartView); ui->dockWidget->setWidget(frame);