Results 1 to 3 of 3

Thread: Non GUI sub class strangely affecting a QMainWindow - Qt4.8 and 5.1

  1. #1
    Join Date
    Jan 2009
    Location
    Midlands UK
    Posts
    62
    Thanks
    6
    Thanked 9 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Non GUI sub class strangely affecting a QMainWindow - Qt4.8 and 5.1

    Hi all,

    I have come across a problem with a custom class of mine with no GUI parts affecting a QMainWindow class which is using it.

    The effect is to mask the main window upper left corner out to screen coords 99,29 so that if I have a button on the main window in the upper left corner the user cannot click it until the mouse is outside the area 0,0 to 99,29. I've checked this on WinXp, Win7 and Qt4.8 and Qt5.1 with MinGw on three different pc's.

    To demonstrate this effect I have built a simple program . A QMainWindow with just a button and a label then a QMouseDown event to show the mouse click position. Next I created a new class ("TestClass") based on QWidget with nothing in it at all. If I add the new class to the mainwindow code I get the above masking effect - any clues to where the problem lies ?


    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QMouseEvent>
    6.  
    7. #include "testclass.h"
    8.  
    9. namespace Ui {
    10. class MainWindow;
    11. }
    12.  
    13. /****************************************************************/
    14. class MainWindow : public QMainWindow
    15. {
    16. Q_OBJECT
    17.  
    18. public:
    19. explicit MainWindow(QWidget *parent = 0);
    20. ~MainWindow();
    21.  
    22. void mousePressEvent(QMouseEvent *event) ;
    23.  
    24. private:
    25. Ui::MainWindow *ui;
    26.  
    27. TestClass *TS ;
    28.  
    29. };
    30.  
    31. /****************************************************************/
    32.  
    33. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. /****************************************************************/
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent), ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9.  
    10. TS = new TestClass(this) ;
    11. }
    12.  
    13. /****************************************************************/
    14. MainWindow::~MainWindow()
    15. {
    16. delete TS ;
    17.  
    18. delete ui;
    19. }
    20.  
    21.  
    22. /****************************************************************/
    23. void MainWindow::mousePressEvent(QMouseEvent *event)
    24. {
    25. ui->MouseMsg->setText(QString::number(event->x()) + ", " + QString::number(event->y()));
    26.  
    27. }
    28.  
    29. /****************************************************************/
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef TESTCLASS_H
    2. #define TESTCLASS_H
    3.  
    4. #include <QWidget>
    5.  
    6. /****************************************************************/
    7. class TestClass : public QWidget
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. explicit TestClass(QWidget *parent = 0);
    13.  
    14. signals:
    15.  
    16. public slots:
    17.  
    18. };
    19.  
    20. /****************************************************************/
    21.  
    22.  
    23. #endif // TESTCLASS_H
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. #include "testclass.h"
    2.  
    3. /****************************************************************/
    4. TestClass::TestClass(QWidget *parent) :
    5. QWidget(parent)
    6. {
    7. }
    8.  
    9. /****************************************************************/
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

  2. #2
    Join Date
    Jul 2011
    Location
    Italy
    Posts
    24
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Non GUI sub class strangely affecting a QMainWindow - Qt4.8 and 5.1

    It's because you create the TestClass widget as a child of the window.
    The widget has no layout and it is placed in the upper left corner.
    You can create the widget without parent:
    Qt Code:
    1. TS = new TestClass() ;
    To copy to clipboard, switch view to plain text mode 

    or just hide it:
    Qt Code:
    1. TS->setVisible(false);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2009
    Location
    Midlands UK
    Posts
    62
    Thanks
    6
    Thanked 9 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Non GUI sub class strangely affecting a QMainWindow - Qt4.8 and 5.1

    Thanks a lot sakya,

    Your simple answer saved a lot of head scratching - I expected a child window to have zero size unless explicitly coded otherwise and had not even considered that answer.

Similar Threads

  1. Replies: 3
    Last Post: 13th November 2011, 08:12
  2. Replies: 10
    Last Post: 21st September 2011, 08:03
  3. Replies: 1
    Last Post: 26th July 2011, 04:09
  4. How to set an attribute without affecting children?
    By dominate in forum Qt Programming
    Replies: 0
    Last Post: 15th February 2011, 14:58
  5. other windows affecting depth buffer in QGLwidget
    By mullwaden in forum Qt Programming
    Replies: 0
    Last Post: 10th September 2008, 13:43

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.