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>
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>