[Solved]Add QWidget item to a custom QMainWindow object from any place.
-
Hi,
I want to add a custom QWidget item built in runtime to my custom QMainWindow. But I lost accesibity to the main windows once initialize at main method:
@
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Title"));
w.show();return a.exec();
}
@So, I have some classes where I need to create and insert to MainWindow some runtime built QWidgets. I tried applying Singleton pattern, but acceding to QMainWindow members is private. So, how can I add QWidgets in runtime? Maybe creating a sub-container and applying Singleton to it (maybe a sub-widget inside the QMainWindow)?
Thank you.
-
Having QMainWindow as a singleton is possible and works without problems, maybe you were doing something wrong.
Just add a slot to your class that will place the widget somewhere in your QLayout and you should be fine.
@
MainWindow::addCustomWidget(QWidget *w)
{
myLayout->addWidget(w);
}
@ -
Hi,
From where would you like to add that widget ?
-
Hi guys, thank you for your fast replies. I'm retrying with Singleton and static method addCustomWidget.
SGaist, I'm trying to add the new Widget from another object, that It's not part of the GUI (just a swaper between logical and GUI parts).
Thank you.
-
If you want to keep logic and GUI separated, better go with emitting a signal:
@
emit newWidget(QWidget *w);
@Then connect it to your slot in the main window. Singletons are nice and easy to use, but they are - IMO - a very sloppy design practice. When using a singleton it's easy to interconnect your classes so much that you are not able to separate them later. With signals it's maybe a bit harder to implement, but you or your colleagues will benefit from it later.
-
Well, I tried with Singleton, at main method (with a fast try, making public static MainWindow member):
@
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
MainWindow::mainW = &w; //Assignation
w.setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Title"));
w.show();return a.exec();
}
@And in Main Window class:
@
class MainWindow : public QMainWindow
{
Q_OBJECT
friend class MainWindow;
public:
explicit MainWindow(QWidget parent = 0);
~MainWindow();
static MainWindow mainW;
(...)
@I get the following issue:
\debug\main.o:-1: In function
Z5qMainiPPc': main.cpp:10: undefined reference to
MainWindow::mainW'
collect2.exe: error: ld returned 1 exit statusI dunno whats happening. includes seems to be Ok.
-
Use "this":http://www.yolinux.com/TUTORIALS/C++Singleton.html for reference.
-
Thank you for your great advice. Im trying that before post more trobules at this point.
Thank you!![quote author="sierdzio" date="1375791508"]If you want to keep logic and GUI separated, better go with emitting a signal:
@
emit newWidget(QWidget *w);
@Then connect it to your slot in the main window. Singletons are nice and easy to use, but they are - IMO - a very sloppy design practice. When using a singleton it's easy to interconnect your classes so much that you are not able to separate them later. With signals it's maybe a bit harder to implement, but you or your colleagues will benefit from it later.[/quote]
-
Finally, I used a Swaper (between logic and GUI) that is Singleton, and uses signals as you shown.
Thank you =)