Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]Add QWidget item to a custom QMainWindow object from any place.
Forum Updated to NodeBB v4.3 + New Features

[Solved]Add QWidget item to a custom QMainWindow object from any place.

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 4.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rhadel
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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);
      }
      @

      (Z(:^

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        From where would you like to add that widget ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Rhadel
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

            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.

            (Z(:^

            1 Reply Last reply
            0
            • R Offline
              R Offline
              Rhadel
              wrote on last edited by
              #6

              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 status

              I dunno whats happening. includes seems to be Ok.

              1 Reply Last reply
              0
              • sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #7

                Use "this":http://www.yolinux.com/TUTORIALS/C++Singleton.html for reference.

                (Z(:^

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  Rhadel
                  wrote on last edited by
                  #8

                  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]

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    Rhadel
                    wrote on last edited by
                    #9

                    Finally, I used a Swaper (between logic and GUI) that is Singleton, and uses signals as you shown.

                    Thank you =)

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved