How to open a new window through QMenuBar?



  • 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!


  • Lifetime Qt Champion

    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 ?

  • Moderators

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


  • @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.


  • Moderators

    @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.



  • @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?


  • Moderators

    @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. :)



  • @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.