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. Two Windows
Forum Updated to NodeBB v4.3 + New Features

Two Windows

Scheduled Pinned Locked Moved General and Desktop
10 Posts 2 Posters 5.5k 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.
  • E Offline
    E Offline
    ExtraLeonard
    wrote on last edited by
    #1

    Sorry if this is another Q with same old A.

    I have MainWindow and with one function it create one another Window. In that Window I have a button and that button should be able to access some properties of MainWindow, like move position or change Window Title. But I couldn't. I cant access it. I've include header file of MainWindow in second Window cpp and h file but it is the same problem. I can see it but compiler report error:

    For example on:
    MainWindow.setWindowTitle("Hi");

    it reports:
    "error: expected unqualified-id before '.' token"

    So where is the problem...???

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MuldeR
      wrote on last edited by
      #2

      MainWindow probably is a class - not an object.

      You can't call non-static methods from a class. You need an instance (object) of that class!

      In your case the obvious solution would be as follows:

      When your instance of MainWindow creates the second window, it needs to pass a pointer to itself into the constructor of the second window. The second window can remember that pointer in some member variable. To access the MainWindow instance at later time, the second window will simply use the pointer it stored before.

      Actually you don't need to code that yourself, because in Qt all Widgets already store a parent pointer :-)

      My OpenSource software at: http://muldersoft.com/

      Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

      Go visit the coop: http://youtu.be/Jay...

      1 Reply Last reply
      0
      • E Offline
        E Offline
        ExtraLeonard
        wrote on last edited by
        #3

        so what is the code for example in order to understand more clearly??

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MuldeR
          wrote on last edited by
          #4

          The basic idea is like this:

          @class A
          {
          public:
          A(void)
          {
          /constructor of A/
          }

          void createB(void)
          {
              m_b = new B(this);
          }
          
          void sayHello(void)
          {
              printf("Hello, this is A talking!\n");
          }
          

          private:
          B *m_b; //Here we'll store the pointer to B, once we create it
          }

          class B
          {
          public:
          B(A *a)
          {
          m_a = a; //remember the pointer to the instance of A!
          }

          void doSomethingWithA(void)
          {
              m_a->sayHello();
          }
          

          private:
          A *m_a; //Here we'll store the pointer to the A who created us
          }@

          But remember that a Qt widget will already store a pointer to it's parent!

          My OpenSource software at: http://muldersoft.com/

          Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

          Go visit the coop: http://youtu.be/Jay...

          1 Reply Last reply
          0
          • E Offline
            E Offline
            ExtraLeonard
            wrote on last edited by
            #5

            You did this on purpose... :D
            OK, if I understand clearly in my case it is like this:

            QMainWindow *m;
            m = MainWindow;
            m->setWindowTitle("Hi");

            Or not?

            Remember, I am beginner

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MuldeR
              wrote on last edited by
              #6

              Well, maybe you should read a basic C++ tutorial first, before you get deeper into Qt.

              Anyway, your line "m = MainWindow" doesn't make any sense. You can't assign a pointer to a class.

              But to make a long story short, in your case the solution could be:
              @void TheSecondWindow::doSomethingWithMainWindow(void)
              {
              MainWindow m = dynamic_cast<MainWindow>(this->parent());
              if(m != NULL)
              {
              m->setWindowTitle(“Hi”);
              }
              else
              {
              printf("Error: Parent was not set or parent is not of type MainWindow!");
              }
              }@

              ...given that you set the parent properly when you create the second window!

              @void MainWindow::createSecondWindow(void)
              {
              TheSecondWindow *m_second = new TheSecondWindow(this);
              }@

              My OpenSource software at: http://muldersoft.com/

              Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

              Go visit the coop: http://youtu.be/Jay...

              1 Reply Last reply
              0
              • E Offline
                E Offline
                ExtraLeonard
                wrote on last edited by
                #7

                OK, thanks... actually I am familiar with dynamic_cast when I use it years ago with Borland C++ Builder in Windows but when I left Windows and switch on Linux everything change for me and also I never use casting in this case... tnx I wil try!

                1 Reply Last reply
                0
                • E Offline
                  E Offline
                  ExtraLeonard
                  wrote on last edited by
                  #8

                  With this:
                  TheSecondWindow *m_second = new TheSecondWindow(this);

                  m_second is showing inside of first Window and that is not what I want but it is working (I have access now)

                  I understand why is acting like that (first is parent of the second) but how to make it not showing inside of the first one?

                  QT is really new to me!

                  TNX for helping :D

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    MuldeR
                    wrote on last edited by
                    #9

                    If you want a widget to be a "top-level" widget, i.e. a widget without a parent, you need to set its parent to NULL. Of course then you won't be able to access the MainWindow instance via parent() anymore. Bummer!

                    Workaround: Simply add your own setter method and a suitable member variable.
                    @class TheSecondWindow
                    {
                    [...]

                    void setMainWindow(MainWindow *m);
                    
                    m_mainWindow *MainWindow;
                    
                    [...]
                    

                    };

                    void TheSecondWindow::setMainWindow(MainWindow *m)
                    {
                    m_mainWindow = m;
                    }

                    void TheSecondWindow::doSomethingWithMain(MainWindow *m)
                    {
                    m_mainWindow->setWindowTitle(“Hi”);
                    }@

                    In the MainWindow you'd change your code like this:

                    @TheSecondWindow *m_second = new TheSecondWindow(NULL);
                    m_second->setMainWindow(this);@

                    My OpenSource software at: http://muldersoft.com/

                    Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                    Go visit the coop: http://youtu.be/Jay...

                    1 Reply Last reply
                    0
                    • E Offline
                      E Offline
                      ExtraLeonard
                      wrote on last edited by
                      #10

                      Cool, look great, will try tomorrow, now is too late (or early) at my place (2:00AM)
                      tnx

                      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