Connect ComboBox signal to my class slot [SOLVED]
-
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.
-
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_OBJECTpublic:
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 indexint 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.
-
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!!
-
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.
-
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.
-
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.
-
"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.
-
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.
-
Don't worry a bit of patience and a good book will do marvels ;)