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. Having form A access form B
Forum Updated to NodeBB v4.3 + New Features

Having form A access form B

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.9k 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.
  • C Offline
    C Offline
    Contempt
    wrote on last edited by
    #1

    In my Qt project I have two UIs (QMainWindow, QDialog) Form A and Form B. How can I make it so that when 'gobutton' on Form B is pressed Form A changes the current index of a tab menu to 1? I have looked at a few things, but they all seem outdated or just didn't work.

    TL;DR What's the easiest way to make a button press in one file trigger an event in another?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      fastest way is a signal & slot.
      http://doc.qt.io/qt-5/signalsandslots.html

      C 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi
        fastest way is a signal & slot.
        http://doc.qt.io/qt-5/signalsandslots.html

        C Offline
        C Offline
        Contempt
        wrote on last edited by
        #3

        @mrjj I tried using slots or signals, but I got mixed up in connecting a signal from one file to a slot in another. The example in the documentation ("A Small Example") does not showcase or go over how it's done when attempting to do cross-file/cross-ui. Is there a better tutorial covering this?

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          Hi
          try this
          https://www.dropbox.com/s/lev1yukhpkgxhbe/myotherdialog.zip?dl=0
          If still unclear let me know and ill try to explain.

          // How it works:
          // We make new signal for dialog to send
          class MyDialog : public QDialog {
            Q_OBJECT
          public:
          ...
          signals:
            void ChangeTab(int index); <<< new signal. 
          ...
          };
          
          // and for GO button we do
          void MyDialog::on_Go_clicked() {
            emit ChangeTab( 1 ); // tell the world
          }
          
          // then in main we do
          void MainWindow::on_ShowDialog_pressed() {
            MyDialog dialog;
          // connect directly from dialogs new signal to the slot for index for the tabwidget
            connect(&dialog, SIGNAL(ChangeTab(int)), ui->tabWidget, SLOT(setCurrentIndex(int)));
            dialog.exec(); // show it
          }
          
          C 1 Reply Last reply
          2
          • mrjjM mrjj

            Hi
            try this
            https://www.dropbox.com/s/lev1yukhpkgxhbe/myotherdialog.zip?dl=0
            If still unclear let me know and ill try to explain.

            // How it works:
            // We make new signal for dialog to send
            class MyDialog : public QDialog {
              Q_OBJECT
            public:
            ...
            signals:
              void ChangeTab(int index); <<< new signal. 
            ...
            };
            
            // and for GO button we do
            void MyDialog::on_Go_clicked() {
              emit ChangeTab( 1 ); // tell the world
            }
            
            // then in main we do
            void MainWindow::on_ShowDialog_pressed() {
              MyDialog dialog;
            // connect directly from dialogs new signal to the slot for index for the tabwidget
              connect(&dialog, SIGNAL(ChangeTab(int)), ui->tabWidget, SLOT(setCurrentIndex(int)));
              dialog.exec(); // show it
            }
            
            C Offline
            C Offline
            Contempt
            wrote on last edited by
            #5

            @mrjj Thanks :D That example worked perfectly. I assume it is the same if I want to append to a TextBox correct?

            mrjjM 1 Reply Last reply
            0
            • C Contempt

              @mrjj Thanks :D That example worked perfectly. I assume it is the same if I want to append to a TextBox correct?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @Contempt

              i assume it is the same if I want to append to a TextBox correct?

              Well almost, but signal and slot have parameters so make sure to match it up.

              Like
              void QTextEdit::append(const QString &text)

              to send a signal to that slot, it must be a string we sent.
              Say our signal from before
              void ChangeTab(int index);
              is no good as that is an int.
              however if we made new signal
              void ChangeStatusText(const QString &text);
              that would match the widgets slot.

              But yes very the same.

              C 1 Reply Last reply
              2
              • mrjjM mrjj

                @Contempt

                i assume it is the same if I want to append to a TextBox correct?

                Well almost, but signal and slot have parameters so make sure to match it up.

                Like
                void QTextEdit::append(const QString &text)

                to send a signal to that slot, it must be a string we sent.
                Say our signal from before
                void ChangeTab(int index);
                is no good as that is an int.
                however if we made new signal
                void ChangeStatusText(const QString &text);
                that would match the widgets slot.

                But yes very the same.

                C Offline
                C Offline
                Contempt
                wrote on last edited by
                #7

                @mrjj Sorry to re-open this thread, but I've one last question whcih my search terms haven't seemed to answer. Is there a way I can emit a signal from something that is not a UI?
                I have a file called functions.cpp/.h which dialog.cpp calls out to after the go button is pressed, but I want to ->append() things to a textBrowser.

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  bludger
                  wrote on last edited by
                  #8

                  Hi, yes that is possible. Make sure your class inherit QObject and contains the Q_OBJECT macro. That way your class will get registered to Qt's metasystem which allows you to send signals.

                  1 Reply Last reply
                  2

                  • Login

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