Signals and slots question
-
@
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
object1 =new Object1(this);
object2 = new BikePage (this);
.....
}//dont forget the brackets...
@One of the problem is in object2, object2 is an instance of the class Object2 and where is the definition of the BikePage class?, it should be:
@
object2 = new Object2(this);
@Btw, in the fuction readSettings, there is an extra bracket "{"
-
I am really trying to understand this and get a grasp. I appreciate you guys taking your time to help me. I had a typo in my post. This:
@MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
object1 =new Object1(this);
object2 = new BikePage (this);
section = Object1 (this);@Should have been this:
@MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
object1 =new Object1(this);
object2 = new Object2 (this);
@
Gerolf, you stated that normally signals are created from the outside. I don't quite understand what you mean by this...was it simply moving the connect statement before the other functions or is there a better way? You also stated, The member object2 of class Object1 is not initialized, it’s anything! Did my typo correct this? Wait..I think I see the issue! I should have put @object2 = new Object2;@ in the constructor of Object1...is that correct?Franz, I think you were talking about me not putting the object2 = new Object2 in the constructor of Object1...correct? You also mention about making connections outside the classes....I don't understand how to do that.Would the connection definition be placed in the mainwindow class?
Vass, Thanks...that was a typo. I appreciate your help.
Volker, I think I am beginning to understand a bit(maybe only a little bit...maybe too many crashes on my motorcycle has blocked the signals!!) I think I understand your example. Let me see if this is correct...fancy was declared as a pointer to a FancyClass object but there was no FancyClass object created (there should have been fancy= new Fancyclass...in the constructor) Am I getting it? And another option for declaring the FooBar class could have been:
@FooBar(FancyClass *fancyClassPointer) {
fancy = fancyClassPointer;
counter = 0;
}@
Lets see if I got this right. By using the constructor the way you did, the FooBar class is called with a pointer to FancyClass, and then you declare the actual pointer variable in the constructor...Am I close? I have purchased and read a few C++ books and I am just starting to try and program with the little bit of knowledge I have. Any suggestions on some good books? I have the new C++ GUI programming book and a C++ programming in 21 days and a couple of others. But I think I learn better by actual application. I didn't see what you were talking about at first. Now I understand what I did wrong(at least I think I do....I will make the changes when I get home and let you know if it works!) Any suggestions on some good books?
Again, to all I REALLY appreciate your guidance in this issue! -
I have another question. I was looking at the suggestion by Gerolf again and I think I realized what you were saying. But to be sure. Is it better practice to create an object of each class in mainwindow (and instantiate it) of ALL the classes that would require signals and slots and then to define all the connections there. In each class, only the signal and slot needs to be defined. This would eliminate the need to create a class (and instantiate it) in each individual class. Is that what you were saying? If so, this seems like a clean approach. I will try it when I get home!
Something like this:
@MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
object1 =new Object1(this);
object2 = new Object2 (this);
object3 = new Object3 (this);connect(this,SIGNAL(restoreCombo(int)),object1, SLOT(restoreCombo(int))); connect(object1, SIGNAL(cmbObject1Change(int)),object2,SLOT(restoreCombo(int)));
connect(object3,SIGNAL(restoreCombo(int)),object1, SLOT(restoreCombo(int)));
ui->MainStackWidget->setCurrentIndex(0);
ui->MainStackWidget->addWidget(object1);
ui->MainStackWidget->addWidget(Object2);
ui->MainStackWidget->addWidget(object3);
readSettings();
}@
And then in each of the classes (Object1, Object2...) I would only need to declare the signals and slots in the header file? -
That's definitly better.
But I suggest that you follow Volker's suggestion first,bq. And now it’s time to head over to a book store and get you some good introduction to C++ to read for the holidays ;-)
This thing with pointzers instances, intialization etc. is really important in C++ and therefore in Qt. It's not neccassary to read the complete stroustroup (which is quite big) but any C++ introduction could help.
-
Hey,
You should first go through this article then follow the above examples or suggestions.
http://doc.qt.nokia.com/latest/signalsandslots.html
Best regards,
-
[quote author="qtrahul" date="1293091094"]Hey,
You should first go through this article then follow the above examples or suggestions.
http://doc.qt.nokia.com/latest/signalsandslots.html
Best regards,[/quote]
Rahul,
The author has mentioned that "I have been reading about signals and slots". And anyone reading about it will definitely have gone through the Qt documentation.
-
poporacer, in that code snippet:
@
FooBar(FancyClass *fancyClassPointer) {
fancy = fancyClassPointer;
counter = 0;
}
@the variable fancy was declared and defined in the class definition (class FooBar { .... }). In the constructor there is a value (that of fancyClassPointer) assigned to it.
It is really, really important to know the differences between declaration, definition and assignment of things in C/C++. These kind of things are way better to learn with small, well commented sample programs than with a full sized application where on has to cover a huge bunch of other questions.
Please, do yourself a favor and read some "Introduction to C++" (not necessarily covering Qt! You can add this later on!) from the beginning(!). And please do not try to abridge things - your programs will end like your motorcycle :-(
-
[quote author="QtK" date="1293093708"]
The author has mentioned that "I have been reading about signals and slots". And anyone reading about it will definitely have gone through the Qt documentation.
[/quote]Unfortunately not (although I do not know for poporacer, to be fair).
-
@
void Object1::on_cmbBox_currentIndexChanged()
{
int id=(ui->cmbBox->itemData(ui->cmbBox->currentIndex()).toInt());
emit cmbObject1IndexChange(id);
}@
it must put int the header as a slot
@
public slots:
void on_cmbBox_currentIndexChanged();
@ -
Thanks for all the help. Yes I have read about Signals and slots. My signals and slots were working, it was a basic C++ error. The documentation shows how signals and slots work in one class not accross different classes. I will continue to read and practice what I have read...Thanks for all your help!
-
A good starting place is the API docs on "QObject::connect() ":http://doc.qt.nokia.com/stable/qobject.html#connect. Basicall it's four parameters to connect():
Pointer to the sender QObject
Name and signature of the sender's signal
Pointer to the receiver QObject
Name and signature of the receiver's slot
Neither of the two pointers needs to be "this". It's quite ok (and often done) to setup a connection between to other objects.
By QObject is meant any QObject based class, i.e. almost all GUI classes (as QWidget is a QObject subclass and thus are all QWigets subclasses).