Results 1 to 5 of 5

Thread: App freezes when new Qlabel(this) in Mainwindow or resize call

  1. #1
    Join Date
    Jun 2013
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default App freezes when new Qlabel(this) in Mainwindow or resize call

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

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: App freezes when new Qlabel(this) in Mainwindow or resize call

    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.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Jun 2013
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: App freezes when new Qlabel(this) in Mainwindow or resize call

    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

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: App freezes when new Qlabel(this) in Mainwindow or resize call

    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
    Qt Code:
    1. QLabel *label = new QLabel("QLabel", this);
    To copy to clipboard, switch view to plain text mode 

    QMainWidget has a special layout, you will need to use setCentralWidget(label) after QLabel creation to make the menu work
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5
    Join Date
    Jun 2013
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: App freezes when new Qlabel(this) in Mainwindow or resize call

    Thanks !

    Sean

Similar Threads

  1. Call MainWindow through QDialog
    By sousadaniel7 in forum Qt Programming
    Replies: 13
    Last Post: 2nd May 2012, 15:35
  2. Replies: 1
    Last Post: 12th April 2011, 09:53
  3. How to call a dialog from a mainwindow
    By luiz4um in forum Qt Programming
    Replies: 26
    Last Post: 29th June 2010, 10:41
  4. MainWindow won't resize, why does this work?
    By weevil in forum Qt Programming
    Replies: 1
    Last Post: 20th June 2010, 07:08
  5. QFile::resize takes forever and freezes the GUI
    By MaximA in forum Qt Programming
    Replies: 8
    Last Post: 27th May 2008, 09:53

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.