PDA

View Full Version : App freezes when new Qlabel(this) in Mainwindow or resize call



seany
8th June 2013, 20:30
Hi

I am trying to put a new Qlabel with new Pixmap on screen when the Window is resized. Pixmap is not shown to reduce it to simplest test.

The problem is when I try to create a new QLabel with new QLabel(this), the application pretty much freezes. Even the Menu drop down bar does not work anymore.
However, if I just have new QLabel(), then the app works. Has this got to do with "this" is not valid at initial resize calls at app initializations ? I noticed if I try to do the same within MainWindow function (not shown below), I get the same behavior. I don't seem to find documentation to this sort of behavior. The code below has some more comments on the problem point.

Trying to understand some core behaviors in Qt. Appreciate your advice.

Thanks

Sean

-------------------------------------- mainwindow.cpp ----------------------------------------------------
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <Qlabel>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_actionActivate_triggered()
{
qDebug() << "Activate";
}

void MainWindow::resizeEvent(QResizeEvent *event)
{
qDebug() << "Resize";

//App freezes. Even on_actionActivate_triggered called from Mainwindow main menu will not work anymore
QLabel *label = new QLabel(this);

//Below will work
//QLabel *label = new QLabel();
}

----------------------------mainwindow.h --------------------------------------

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void on_actionActivate_triggered();

private:
Ui::MainWindow *ui;

protected:
void MainWindow::resizeEvent(QResizeEvent *event);
};

#endif // MAINWINDOW_H

----------------------------- mainwindow.h ------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget"/>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>21</height>
</rect>
</property>
<widget class="QMenu" name="menuTest">
<property name="title">
<string>Test</string>
</property>
<addaction name="actionActivate"/>
</widget>
<addaction name="menuTest"/>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<action name="actionActivate">
<property name="text">
<string>Activate</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

Santosh Reddy
8th June 2013, 23:57
It is not safe, in fact can be very unstable to create new objects in resizeEvent(), as it is expected to be called many times during the a QWidget's lifetime. One can still survive with small objects being created in resizeEvent() but, definitely not a new QWidget (QLabel in this case) again.

One simple approach would be use an existing QLabel and just change / resize the pixmap in the QLabel.

seany
9th June 2013, 01:49
Hi Santhosh

Note that if I do the same code in MainWindow function, it's the same behavior. So, it isn't an issue of a reset called many times. In fact try out my code on the first resize event and you will see the issue.

Thanks

Sean

Santosh Reddy
10th June 2013, 08:53
Ok your problem is not with App freezing, you App works as expected when QLabel is created in resizeEvent(). the problem is when a widget is created with a parent and not added to a layout, it is my default placed at parent's (0,0), which in this is case is overlapping with the menu bar and you cannot click on menu any more.

Just try this and you can see for yourself

QLabel *label = new QLabel("QLabel", this);

QMainWidget has a special layout, you will need to use setCentralWidget(label) after QLabel creation to make the menu work

seany
16th June 2013, 09:49
Thanks !

Sean