PDA

View Full Version : Required a pointer "this" to QWidget inside a component class.



ZikO
10th August 2012, 15:52
Hello,

I have recently found a problem when I wanted to use a Qt dialog window to show information or ask for a file path. The problem is that I need to do it inside a constructor of another class. If I had an arbitrary code below, what would I need put in the line 43 (inside a constructor of class A) to open QMessageBox that requires a pointer "this" to my window class? If I inherited from MyWindow, would something like &(static_cast<MyWindow&>(*this)) work?

Thanks


#include <QApplication>
#include <QMessageBox>
#include <QWidget>

////////////////////////////////////////
class MyWindow : public QWidget {
Q_OBJECT
public:
MyWindow(QWidget* parent = 0);

public slots:
signals:
};

class A {
int a;
double b;
public:
A(int _a = 0, double _b = 0.0);
};
////////////////////////////////////////

///// ===== main function ====
int main(int argc, char* argv[]) {
QApplication app(argc,argv);

return app.exec();
}
///// ===== end of main ======

///// MyWindow //////////////////////////
MyWindow::MyWindow(QWidget* parent)
: QWidget(parent) {
// Create Layout, actions, menus, etc.
A object_A(10,20.5);
}
///// End of MyWindow ///////////////////

///// A /////////////////////////////////
A::A(int _a, double _b) : a(_a), b(_b) {
// Some problems here and information need to be shown:
QMessageBox::information(
this,
tr("Title"),
tr("Information to be shown"));
}
///// End of A //////////////////////////


PS. I did not really run this code. It is only to show the problem.

amleto
10th August 2012, 16:31
why not just use NULL?

Otherwise change A like


class A {
int a;
double b;
public:
A(int _a = 0, double _b = 0.0, QWidget* parentForMsgBox = 0);
};

...

ZikO
10th August 2012, 19:41
Hello,

OK. Thanks for this advice. I usually do so providing a full code. I did think my code was too long and it would take the whole page. Also, the code was separated into several headers / source files due to a few classes / structures I have implemented. I just assumed the problem would be to trivial but I will follow this rule next time.

As to the problem, it works now. I came here because I am still a newbie programmer and I did expect the problem would be solved in a very easy way. I really should have found it by myself :/ somehow still not experienced enough to realise that extra argument would solve the problem.

Thank you.