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. How to open a new window through QMenuBar?
Forum Updated to NodeBB v4.3 + New Features

How to open a new window through QMenuBar?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 2.5k Views 2 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
    El3ctroGh0st
    wrote on last edited by
    #1

    Hello,
    I am trying to manage to open a custom new window through a QAction. I have a couple of questions. Firstly, what should template should I choose if I want to setup a new window for changing the settings? Do I just create a new Designer Form Class and then choose a QWidget? Or should I choose something else?
    And secondly, how do I handle the changes made in this settings window? For instance, right now I am working on a textFinder program, which highlights the matches in yellow. I want to implement a setting that allows you to change that colour. But lets say someone changes that colour to red in the settings window, how do I notify the MainWindow that a change has been made?
    Thirdly, how do I show that window? I've tested it with a QDialogWindow (named "settings") and wrote in the constructor of MainWindow:

    connect(ui->actionHelp, &QAction::triggered, this, &textFinder::showSettingsWindow);
    

    and then I defined the function

    void textFinder::showSettingsWindow()
    {
        settings *settingsWindow = new settings(); //the QDialogWindow
        settingsWindow->show();
    }
    

    but it didnt seem to work out. I hope someone can answer me those questions!

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

      Hi,

      1. a QDialog based widget
      2. You give your settings widget a clean interface with signals that you will connect to in order to propagate the changes.
      3. What didn't work ? What did you expect ? What did you got ?

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

      E 1 Reply Last reply
      3
      • E El3ctroGh0st

        Hello,
        I am trying to manage to open a custom new window through a QAction. I have a couple of questions. Firstly, what should template should I choose if I want to setup a new window for changing the settings? Do I just create a new Designer Form Class and then choose a QWidget? Or should I choose something else?
        And secondly, how do I handle the changes made in this settings window? For instance, right now I am working on a textFinder program, which highlights the matches in yellow. I want to implement a setting that allows you to change that colour. But lets say someone changes that colour to red in the settings window, how do I notify the MainWindow that a change has been made?
        Thirdly, how do I show that window? I've tested it with a QDialogWindow (named "settings") and wrote in the constructor of MainWindow:

        connect(ui->actionHelp, &QAction::triggered, this, &textFinder::showSettingsWindow);
        

        and then I defined the function

        void textFinder::showSettingsWindow()
        {
            settings *settingsWindow = new settings(); //the QDialogWindow
            settingsWindow->show();
        }
        

        but it didnt seem to work out. I hope someone can answer me those questions!

        A Offline
        A Offline
        ambershark
        wrote on last edited by
        #3

        @El3ctroGh0st Easiest way to handle this is a modal dialog, then you don't need signals for the response since you can check it and act on it as soon as the window finishes.

        void textFinder::showSettingsWindow()
        {
           settings dlg;
           if (dlg.exec() == QDialog::Accepted)
           {
              // process your color update here
           }
        }
        

        My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

        1 Reply Last reply
        2
        • SGaistS SGaist

          Hi,

          1. a QDialog based widget
          2. You give your settings widget a clean interface with signals that you will connect to in order to propagate the changes.
          3. What didn't work ? What did you expect ? What did you got ?
          E Offline
          E Offline
          El3ctroGh0st
          wrote on last edited by
          #4

          @SGaist Thanks for your tips! You really helped me. Also, the problem just was that I was connecting the signal of the wrong Action.

          @ambershark That's neat, I've implemented it that way and it seems to work out. Thanks a lot! Now I just have to figure out how to make it save the settings (I'm not talking about saving it when I restart the whole program, but just when I reopen the settings Window.) but I'll wait until tomorrow until I make the attempt.

          Thanks again to you two.

          A 1 Reply Last reply
          0
          • E El3ctroGh0st

            @SGaist Thanks for your tips! You really helped me. Also, the problem just was that I was connecting the signal of the wrong Action.

            @ambershark That's neat, I've implemented it that way and it seems to work out. Thanks a lot! Now I just have to figure out how to make it save the settings (I'm not talking about saving it when I restart the whole program, but just when I reopen the settings Window.) but I'll wait until tomorrow until I make the attempt.

            Thanks again to you two.

            A Offline
            A Offline
            ambershark
            wrote on last edited by
            #5

            @El3ctroGh0st For that you just set them before you open the dialog... something like:

            settings dlg;
            dlg.setColor(color);
            dlg.exec();
            

            Then in your settings dialog you just have the setColor() function that tells it the default when it displays.

            The other option is to allocate settings on the heap and don't destroy it, just hide and show it. Then it will remember it's setting during execution. Not after closing the app though.

            My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

            E 1 Reply Last reply
            2
            • A ambershark

              @El3ctroGh0st For that you just set them before you open the dialog... something like:

              settings dlg;
              dlg.setColor(color);
              dlg.exec();
              

              Then in your settings dialog you just have the setColor() function that tells it the default when it displays.

              The other option is to allocate settings on the heap and don't destroy it, just hide and show it. Then it will remember it's setting during execution. Not after closing the app though.

              E Offline
              E Offline
              El3ctroGh0st
              wrote on last edited by
              #6

              @ambershark Okay, I will try the latter one. But since I am not yet very confident with C++ yet, I just want to make sure: With storing it on the heap you basically mean to create the pointer to the window outside the function, right? And if I'm not wrong I also have to delete it in the destructor to prevent memory leaks?

              A 1 Reply Last reply
              0
              • E El3ctroGh0st

                @ambershark Okay, I will try the latter one. But since I am not yet very confident with C++ yet, I just want to make sure: With storing it on the heap you basically mean to create the pointer to the window outside the function, right? And if I'm not wrong I also have to delete it in the destructor to prevent memory leaks?

                A Offline
                A Offline
                ambershark
                wrote on last edited by
                #7

                @El3ctroGh0st That would be correct... However with Qt you don't need to worry about that. Just set it's parent and when the parent gets cleaned it will clean it up as well.

                So basically in your textFinder class you would do:

                class textFinder : public QWidget
                {
                Q_OBJECT
                
                public:
                   textFinder(QWidget *parent = nullptr) : QWidget(parent)
                   {
                      settingsDialog = new settings(this);
                   }
                
                private:
                   settings *settingsDialog;
                };
                

                Then when you want to use it, just settingsDialog->show().

                Also standard class names in C++ are camelcase capitals like Qt does it. I.e. Settings for your class or SettingsDialog something like that. Not a big deal but when you lower case an object experienced coders will confuse that as a function or variable at a glance. :)

                My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                E 1 Reply Last reply
                4
                • A ambershark

                  @El3ctroGh0st That would be correct... However with Qt you don't need to worry about that. Just set it's parent and when the parent gets cleaned it will clean it up as well.

                  So basically in your textFinder class you would do:

                  class textFinder : public QWidget
                  {
                  Q_OBJECT
                  
                  public:
                     textFinder(QWidget *parent = nullptr) : QWidget(parent)
                     {
                        settingsDialog = new settings(this);
                     }
                  
                  private:
                     settings *settingsDialog;
                  };
                  

                  Then when you want to use it, just settingsDialog->show().

                  Also standard class names in C++ are camelcase capitals like Qt does it. I.e. Settings for your class or SettingsDialog something like that. Not a big deal but when you lower case an object experienced coders will confuse that as a function or variable at a glance. :)

                  E Offline
                  E Offline
                  El3ctroGh0st
                  wrote on last edited by
                  #8

                  @ambershark I see... Thanks again! And no worries, I usually write classes with capitals, but I somehow forgot it for this one... But I've fixed it already.

                  1 Reply Last reply
                  1

                  • Login

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