Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Connect ComboBox signal to my class slot [SOLVED]
Forum Updated to NodeBB v4.3 + New Features

Connect ComboBox signal to my class slot [SOLVED]

Scheduled Pinned Locked Moved Qt Creator and other tools
19 Posts 3 Posters 8.0k 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.
  • P Offline
    P Offline
    phil_n
    wrote on last edited by
    #10

    I'm not sure myself. I used the designer in creator to make a gui that contains only the comboBox and a label mainly to get to understand how to connect the gui widgets to c++ logic and then once I get that, to develop a slightly less trivial app where I use the designer to create the gui and c++ to handle the (simple) logic.

    Trying to go one step at a time I thought I would connect the comboBox to phraseChanged and then use something like qdebug or cout from inside phraseChanged to see where I was. When satisfied that that was working then I would return the new phrase to the label, expecting to use the changePhrase signal of PhraseChanger.

    In main I thought I would need to instantiate an instance of PhraseChanger so that the connection would be made and the logic handling methods would be available when I can figure out how to use them.

    I don't know if that is a good idea and, if so, whether a PhraseChanger object should be created under main or app_1_mainwindow. So I guess that brings up the question: Should PhraseChooser be instantiated in main or in MainWindow (if at all) and (if you have time) why?

    Thanks.

    Where would we be without Earth?

    1 Reply Last reply
    0
    • P Offline
      P Offline
      phil_n
      wrote on last edited by
      #11

      Here's what I have now:

      app_1_mainwindow.h
      @#ifndef APP_1_MAINWINDOW_H
      #define APP_1_MAINWINDOW_H

      #include <QMainWindow>

      namespace Ui
      {
      class app_1_MainWindow;
      }

      class app_1_MainWindow : public QMainWindow
      {

      Q_OBJECT

      public:

      explicit app_1_MainWindow(QWidget *parent = 0);
      ~app_1_MainWindow();

      private:

      Ui::app_1_MainWindow *ui;

      };

      #endif // APP_1_MAINWINDOW_H
      @
      app_1_mainwindow.cpp
      @#include "app_1_mainwindow.h"
      #include "ui_app_1_mainwindow.h"

      app_1_MainWindow::app_1_MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::app_1_MainWindow)
      {

      ui->setupUi(this);

      }

      app_1_MainWindow::~app_1_MainWindow()
      {

      delete ui;

      }
      @
      phrasechooser.h
      @#ifndef PHRASECHOOSER_H
      #define PHRASECHOOSER_H

      #include <QObject>
      #include <iostream>

      using namespace std;

      class PhraseChooser : public QObject
      {
      Q_OBJECT

      public:

      PhraseChooser();

      protected:

      QList<QString> _phrases;
      int _currentIndex;

      public slots:

      void changePhrase(int index);

      signals:

      void phraseChanged(const QString& newPhrase);

      };

      #endif // PHRASECHOOSER_H
      @
      phrasechooser.cpp
      @#include "phrasechooser.h"

      PhraseChooser::PhraseChooser()
      {

      _phrases
      <<tr("Now is the time for all good men to come to the aid of the party.")
      <<tr("He who laughs last, laughs best.")
      <<tr("No time to do it right but plenty of time to do it twice.")
      <<tr("The greatest journey begins with a single step.")
      <<tr("Judge not and ye shall not be judged.")
      <<tr("A bird in the hand is worth two in the bush.")
      <<tr("Money can't buy happiness.")
      <<tr("The best things in life are free.")
      <<tr("If you're not happy with what you have - you'll never be happy.")
      <<tr("Be here now. Be. Here. Now.");

      connect(ui->cb01,SIGNAL(currentIndexChanged(int)), this, SLOT(changePhase(int)));

      }

      void PhraseChooser::changePhrase(int index)
      {

      if(_currentIndex != index)
      {
      _currentIndex = index;
      emit phraseChanged(_phrases.at(index));
      }

      // cout << "Hello?";

      }
      @
      and main.cpp
      @#include "app_1_mainwindow.h"
      #include "phrasechooser.h"
      #include <QApplication>

      // recieves a signal containing the current index of the combo box
      // returns a string to the label corresponding to the index

      int main(int argc, char *argv[])
      {

      QApplication app(argc, argv);
      app_1_MainWindow main_w;

      main_w.show();

      return app.exec();

      }
      @
      and the error
      @/home/phil/projects/qt/various/app_1/app_1/phrasechooser.cpp:18: error: 'ui' was not declared in this scope
      connect(ui->cb01,SIGNAL(currentIndexChanged(int)), this, SLOT(changePhase(int)));
      @
      I've tried all sorts of locations for the connect code and headers but I keep getting the same error. Anybody have any idea why this is happening?

      Thanks.

      Where would we be without Earth?

      1 Reply Last reply
      0
      • mranger90M Offline
        mranger90M Offline
        mranger90
        wrote on last edited by
        #12

        "ui" is a member of "class app_1_MainWindow".
        "ui" is NOT a member of "class PhraseChooser".
        Hence it is undefined in the connect statement.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          phil_n
          wrote on last edited by
          #13

          OK. Iv'e tried variations on this theme (in phrasechooser.cpp):

          @
          // #include "app_1_mainwindow.h"
          // class ; - forward declaration?
          @

          and these (in various combinations with above):

          @
          connect(app_1_mainwindow->ui->cb01...
          connect(app_1_mainwindow::ui->cb01...
          @

          etc... and I still keep getting errors.

          So how the heck do I resolve 'ui' in this context?

          I'm old, my hair's starting to fall out, I can't afford to pull any more of it out!!

          Where would we be without Earth?

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

            ui is a private member of app_1_mainwindow.

            You're doing it from the wrong end. PathChooser should not know anything about app_1_mainwindow or try to connect to it. It's what we could call a helper class.

            On the other hand, app_1_mainwindow will use PathChooser, so it's its responsibility to do the connections and use what PathChooser provides. So make it a member variable of app_1_mainwindow and you should be good to go.

            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
            • P Offline
              P Offline
              phil_n
              wrote on last edited by
              #15

              Thanks again SG.

              As you can see I'm no expert at either end of this - Qt or c++ - so when you say 'member variable' all that pops into my head is ints or strings or stuff like that. I'm stuck on thinking I've made a class - PhraseChooser - and that classes aren't members of other classes (other than through inheritance) so I don't understand when you say 'make it a member variable'. I'm not disputing, I'm sure you know more about this than I ever will - I just don't understand.

              Where would we be without Earth?

              1 Reply Last reply
              0
              • P Offline
                P Offline
                phil_n
                wrote on last edited by
                #16

                It works!

                I figured "member variable" meant something like this:

                @#ifndef APP_1_MAINWINDOW_H
                #define APP_1_MAINWINDOW_H

                #include <QMainWindow>
                #include "phrasechooser.h"

                namespace Ui
                {
                class app_1_MainWindow;
                }

                class app_1_MainWindow : public QMainWindow
                {

                Q_OBJECT

                public:

                explicit app_1_MainWindow(QWidget *parent = 0);
                ~app_1_MainWindow();

                private:

                Ui::app_1_MainWindow *ui;
                PhraseChooser chooser;

                };

                #endif // APP_1_MAINWINDOW_H
                @
                and then this in the cpp:
                @connect(ui->cb01,SIGNAL(currentIndexChanged(int)), &chooser, SLOT(changePhrase(int)));
                connect(&chooser, SIGNAL(phraseChanged(QString)), ui->lbl01,SLOT(setText(QString)));
                @
                ...and it worked!

                Thanks again. Now I can sleep tonight.

                Where would we be without Earth?

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

                  "C++":http://www.cplusplus.com can help you getting started better. Don't try to run before you walk with C++, it's the quickest way to shoot yourself in the foot.

                  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
                  • P Offline
                    P Offline
                    phil_n
                    wrote on last edited by
                    #18

                    Thanks again. I did some college level programming courses in the 70's and 80's but never used it and forgot most of it. Started with Waterloo MicroBASIC, TurboPASCAL, straight into C++ (not realizing I should probably have learned C first) and even a full semester course on C++ pointers. I'm hoping some of it will come back to me as I attempt to use it again and with the help of people like you and your cohorts.

                    I'm sure I'll be back again with more lost-in-the-fog-where-am-I questions soon.

                    Where would we be without Earth?

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

                      Don't worry a bit of patience and a good book will do marvels ;)

                      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

                      • Login

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