Qt window resize event after resize is done - c++

I have a QtChart in a QDialog and I use a simple QWidget to show it on the screen. I need to resize this hart whenever the dialog window is resized by user.
This is how I add the chart to the dialog (in constructor):
// Setup chart view to show the chart
mChartView = new QChartView(mChart, ui->widget);
mChartView->setParent(this);
mChartView->resize(ui->widget->size());
mChartView->setRenderHint(QPainter::Antialiasing);
I have overrided the resizeEvent of the QDialog in my own dialog:
void CurveDialog::resizeEvent(QResizeEvent *event)
{
mChartView->resize(ui->widget->size());
}
This works, and the chart gets resized...but the problem is it is terribly slow! because it will resize for all the steps that user drags the window corner to resize it!
How can I do the resize only when there resize is done? I wanted to use a timer but this looks like a dirty hack! any better ideas?

Qt provides a layout system to manage the geometries of child widgets within a widget. A layout will arrange the size and the position of each child to ensure that it will take all the available space.
The layout will automatically resize the child widgets when the parent is resized:
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
QDialog* dialog = new QDialog();
QVBoxLayout* layoutDialog = new QVBoxLayout(dialog);
QWidget* widget = new QWidget();
QVBoxLayout* layoutWidget = new QVBoxLayout(widget);
layoutDialog->addWidget(widget);
layoutWidget->addWidget(chartView);
dialog->exec();

Related

Is there anyway to add a widget and set its geometry on a qvboxlayout?

When a qboxlayout is created, any widget added to it would in its layout, and I create a widget and set its geomoetry, then add to the widget, the geometry doesn't work.
This is code:
QVBoxLayout *layout = new QVBoxLayout(centralWidget);
QWidget *widget1 = new QWidget();
QWidget *widget2 = new QWidget();
layout->addWidget(widget1);
layout->addWidget(widget2);
QWidget *widget3 = new QWidget();
widget3->setGeometry(0, 0, 100, 100);
layout->addWidget(widget3);
This is what I want to realize
I'm not sure to understand your question exactly, but if you want to define to size of your widget in the layout, you can maybe try widget3->setFixedSize(..). This should set both the maximum and minimum size of the widget.
However, the position will still be managed by the layout.
To have a hand on the position, you can use the Qt::Alignment flags in the addWidget() function.
Otherwise, if you really want to use the geometry directly, you should not add the widget to the layout, but simply create the widget3 with the centralWidget as parent.

Change QT layout background

Since QVBoxLayout has no a setStylesheet method, I thought this would made the trick:
QWidget *window = new QWidget(this);
window->setStyleSheet("background-image:url(:/images/sky.jpg);font-size:18px;");
QVBoxLayout * layout = new QVBoxLayout(window);
layout->addWidget(widg1);
layout->addWidget(widg2);
setLayout(layout);
Sadly, only a small rectangle of background image appears, not covering entire window. How could I do it?
You can set stylesheet to the central widget of your main window. In the example you can put window into some other layout.

set Screen for qDockWidget in QMdiArea in Qt 5

My App uses a QMdiArea is instantiated in a QMainWindow. Its child windows are QScrollArea. Each of them have QDockWidget as child, instantiated upon creation of the QScrollArea.
The QDockWidget are set as floatable.
In multi GPU and multi screen systems, they do not first show up in the same screen. How can I set them floatable and appear in the same screen as the QMdiArea, since QDockWidget do not have the "setScreen" property?
Here is how I set things up within the QMainWindow:
mdiArea = new QMdiArea;
setCentralWidget(mdiArea);
// ImScrollArea inherits a QScrollArea.
ImScrollArea* dropImScrollArea = new ImScrollArea(urlList);
QMdiSubWindow *newSubWindow = new QMdiSubWindow;
newSubWindow->setWidget(dropImScrollArea);
newSubWindow->setAttribute(Qt::WA_DeleteOnClose);
mdiArea->addSubWindow(newSubWindow);
newSubWindow->show();
addDockWidget(Qt::BottomDockWidgetArea, dropImScrollArea->getDock());
dropImScrollArea->getDock()->setFloating(true);
dropImScrollArea->getDock()->show();
The QDockWidget within the QScrollArea is defined as:
dock = new QDockWidget(windowTitle, this);
dock->setFeatures(QDockWidget::NoDockWidgetFeatures);
dock->setFeatures(QDockWidget::DockWidgetMovable);
dock->setFeatures(QDockWidget::DockWidgetFloatable);
dock->setAllowedAreas(Qt::BottomDockWidgetArea | Qt::RightDockWidgetArea);

QWidget within scrollarea

I have a QWidget that I want to include within a scroll-area so that when the designated QWidget size is exceeded vertically, the user can scroll up and down to see more.
QWidget renameWidget;
QScrollArea scrollarea.
How do I go about doing this? I set the widget inside the scroll-area on the UI editor but it didn't work.
Any ideas?
Thanks.
Think of QScrollArea as another layout. Add the scroll area to your main widget and put everything else inside it with setWidget().
QScrollArea is QWidget, so you can even use it as a top level widget:
QScrollArea *scrollArea = new QScrollArea();
scrollArea->resize(250, 250);
QWidget *widget = new QWidget(scrollArea);
widget->setBackgroundRole(QPalette::Dark);
widget->resize(200, 200);
scrollArea->setWidget(widget);
scrollArea->show();
QScrollArea provides a scrolling view onto another widget. It is used to display the contents of a child widget within a frame. If the widget exceeds the size of the frame, the view can provide scroll bars so that the entire area of the child widget can be viewed.
An example:
QScrollArea *scrollArea = new QScrollArea(this);
scrollArea->setBackgroundRole(QPalette::Dark);
scrollArea->setWidget(renameWidget);

QPaintEvent painting region in Qt?

This is a basic doubt regarding the Painting done in Qt.
I have a QScrollArea as my centralWidget in Main Window of my Application. I have added a QFrame frame to the scrollarea. The Layout of the QFrame is QGridLayout.
When I add widgets to the layout like this:
MainWindow::AddLabel()
{
setUpdatesEnabled(false);
QGridLayout *myGrid = (QGridLayout *)ui->frame->layout();
for(int i = 0; i < 1000; i++)
{
QLabel *label = new QLabel();
QString str;
str.SetNum(i);
label->SetText(str);
myGrid->AddWidget(label, 0, i, 0);//add label to i'th column of row 0
}
setUpdatesEnabled(true);
repaint();
}
Please dont worry about the memory leak as it is not the focus of the question.
So my doubt's are:
Is setting the updates disabled during adding widgets to layout any helpful?
Even if I maximise the window not all the QLabel's will be visible to me. So when the code flow leaves the above function & goes to the event loop then are all the QLabel's & the enormous area of QFrame painted? Or only those QLabel's which are visible & only that much area of QFrame which is visible painted?
If you are using a form (.ui) then the widgets inside the ui are not children of your widget MainWindow. Well , setUpdatesEnabled() only affect the current widget as well as its children, so the object ui->frame will still receive updates after myGrid->AddWidget. Change to
ui->frame->setUpdatesEnabled(false);
...
ui->frame->setUpdatesEnabled(true);
Btw, when you enable updates, then screen will be updated. So you dont need to call repaint(); on any widget.