PDA

View Full Version : Non GUI sub class strangely affecting a QMainWindow - Qt4.8 and 5.1



SteveH
24th August 2013, 19:34
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 ?



#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMouseEvent>

#include "testclass.h"

namespace Ui {
class MainWindow;
}

/************************************************** **************/
class MainWindow : public QMainWindow
{
Q_OBJECT

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

void mousePressEvent(QMouseEvent *event) ;

private:
Ui::MainWindow *ui;

TestClass *TS ;

};

/************************************************** **************/

#endif // MAINWINDOW_H



#include "mainwindow.h"
#include "ui_mainwindow.h"

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

TS = new TestClass(this) ;
}

/************************************************** **************/
MainWindow::~MainWindow()
{
delete TS ;

delete ui;
}


/************************************************** **************/
void MainWindow::mousePressEvent(QMouseEvent *event)
{
ui->MouseMsg->setText(QString::number(event->x()) + ", " + QString::number(event->y()));

}

/************************************************** **************/


#ifndef TESTCLASS_H
#define TESTCLASS_H

#include <QWidget>

/************************************************** **************/
class TestClass : public QWidget
{
Q_OBJECT

public:
explicit TestClass(QWidget *parent = 0);

signals:

public slots:

};

/************************************************** **************/


#endif // TESTCLASS_H



#include "testclass.h"

/************************************************** **************/
TestClass::TestClass(QWidget *parent) :
QWidget(parent)
{
}

/************************************************** **************/

sakya
25th August 2013, 10:04
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:

TS = new TestClass() ;

or just hide it:

TS->setVisible(false);

SteveH
25th August 2013, 19:29
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.