Problem with signal and slot
-
I'm totally new to QT but I have studied lots of its' documents. However, when I come to code, lots of problem show up.
But I just can't figure it out what happened to these code:
@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "model.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{ui->setupUi(this);
model *m= new model;
connect(ui->horizontalSlider,SIGNAL(this->valueChanged(int)),m,SLOT(m.setTemp(double) ) );
}
MainWindow::~MainWindow()
{
delete ui;
}
@I don't know why the compiler is always complaining about the @connect() method@
QObject is an inaccessible base of 'model'
I'll appreciate you if any examples code with explanations are provided. (signal and slots)
Thanks! -
Hi and welcome to devnet,
You have several errors:
-
The signals and slots signature must match when using thie version of connect
-
You must not give the object in SIGNAL nor in SLOT, just the method.
-
What is model ? a private QObject ?
-
-
I beleive you declare your class like this:
@
class model : QObject
@But in that case you forgot the public keyword, otherwise it default to private inheritence which is not what you want. So it should be:
@
class model : public QObject
@Then, like SGaist said, in the SIGNAL and SLOT, you have to put the exact signature, and the argument must match
@
connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),m,SLOT(setTemp(int) ) );
@Which means you need to change setTemp to take an int and not a double. However, if you are using Qt5, i recommand the other syntax which allow automatic conversion of the argument from int to double:
@
connect(ui->horizontalSlider,&QSlider::valueChanged,m,&model::setTemp );
@ -
SGaist, Olivier Goffart:
Thank you so much. I really made the mistake which I make a private inheritance. Btw: I found the new sytnax documentation:
http://qt-project.org/wiki/New_Signal_Slot_SyntaxThanks!!
-
Well, I haven't finished my design yet. My plan is to let user drag the slide to set the tempertaure variable. And the the mehtod setTemp(int) in the class model will call another signal method
changeColor() to set a QWidget (I don't know what widget can I use, a label?) to show the color.Just like:
@
#ifndef MODEL_H
#define MODEL_H
#include <QObject>
class model:public QObject
{
public:
model();
void setTemp(int temparature);
private:
double temparature;signals:
void changeColor();
};#endif // MODEL_H
But I have several quesions here:
@
-
The function arguments in the SIGNAL() and SLOT() should be equal but I don't have an argument for the method changeColor();
-
I want to use the method changeColor() to decide the color to be represented with some if else judgement. But I think it's a little redundant. I ask for a good design. Any good suggestion?
-
Should I write the connect function in the MainWindow class or where?
-
-
You can e.g. add a QColor parameter to changeColor so you have only once place that handles that.
Yes, a QLabel is fine for that.
Where will your QLabel be ?
-
I will put my QLabel in MainWindow class. But I how can resolve the signal and slot problem? They don't have correspoding arguments.
-
Then add a slot to your MainWindow that takes a QColor parameter and update the QLabel content in there.
-
Ok, should I define the slot function in MainWindow or can I define the slot function in another class like A and then inheriting A?
I don't want to put all the code together in one class~
-
If you are thinking about inheriting both from QMainWindow and from A then no, you can't you can only inherit from one QObject and it also must be the first class to be inherited from.
-
All right. Thank you.