Solved Using Signals and Slots
-
@koahnig said in Using Signals and Slots:
To add to jsulm
you need code to emit the signal or you can connect it to another signal (e.g. of QSlider).
isn't
emit
a placeholder? Andemit mySignal();
acts the same asmySignal();
But it gets quickly confusing if you omit the
emit
! -
@J.Hilk He means that the signal needs to be emitted somewhere
-
@JimmyMars How did you add Slider class? Manually or using QtCreator wizard?
-
@jsulm
I did it manually. -
@JimmyMars Then either read and follow http://doc.qt.io/qt-5.9/moc.html or do it using QtCreator wizard.
QObject derived classes need special handling using moc meta compiler from Qt. This isusually done for you, but not if you add a class manually. -
@JimmyMars Actually it should work without any manual work if you use qmake.
Can you show your pro file? -
I think there needs to be:
QT += core gui widgetsI have it currently like this.
HEADERS= slider.h SOURCES= main.cpp slider.cpp OTHER_FILES += \ readme.txt QT += widgets
-
TEMPLATE = app QT += core gui widgets
-
@jsulm
That solved it.
Thanks for helping me out. -
To solve this problem, you need to follow the steps below.
-
In slider.cpp, change this expression emit m_slider->valueChanged(newValue) to emit valueChanged(newValue).
-
In slider.cpp, remove the implementation of valueChanged method. This means you need to remove the following part.
void Slider::valueChanged(int newValue){
m_slider->valueChanged(newValue);
}Build it and you will find everything goes well.
Why?
When you declare valueChanged in slider.h with signals Macro as well as QObject Macro. It means Qt's moc compiler will help you deal with the implementation of valueChanged. So you shouldn't implement it. Unlike other C++ code, when you declare some method in .h file, you need to implement it in .cpp file. Actually, Qt follows this, but it has already made it done.
-