Problems while using QPushButton
-
Hi everyone, i'm having some trouble using QPushButton, what i'm trying to do is to create a Button that, once clicked, makes my slider "value" 150. Here is the code i wrote, but, this is still not working :)
@#include <QtGui/QApplication>
#include <QSlider>
#include <QPushButton>
#include <QHBoxLayout>
#include <QObject>int main(int argc, char *argv[])
{QApplication app(argc, argv); QWidget *finestra= new QWidget; finestra->setWindowTitle("Player"); QSlider *sli= new QSlider; sli->setRange(0,150); QPushButton *play= new QPushButton("Play"); QObject::connect(play, SIGNAL(clicked()), sli, SLOT(setValue(150))); QHBoxLayout *lay= new QHBoxLayout; lay->addWidget(sli); lay->addWidget(play); lay->addWidget(te); finestra->setLayout(lay); finestra->show(); return app.exec();
}
@Any idea?:) Thanks for the attention :)
-
It of course will not work. Try to read "about signals and slots in Assitant":http://doc.qt.nokia.com/4.7/signalsandslots.html . It will help you to understand better that mechanism.
-
Check the simple example of "QPushButton at our wiki":http://developer.qt.nokia.com/wiki/How_to_Use_QPushButton. Follow the code snippet and modify method handleButton() to change the value of your slider.
-
@
#include <QtGui/QApplication>
#include <QSlider>
#include <QPushButton>
#include <QHBoxLayout>
#include <QObject>
#include <QSignalMapper>int main(int argc, char *argv[])
{
QApplication app(argc, argv);QWidget *finestra= new QWidget; finestra->setWindowTitle("Player"); QSlider *sli= new QSlider; sli->setRange(0,150); QPushButton *play= new QPushButton("Play"); const int value = 150; QSignalMapper signalMapper; QObject::connect(play, SIGNAL(clicked()), &signalMapper, SLOT(map())); signalMapper.setMapping(play, value); QObject::connect(&signalMapper, SIGNAL(mapped(int)), sli, SLOT(setValue(int))); QHBoxLayout *lay= new QHBoxLayout; lay->addWidget(sli); lay->addWidget(play); finestra->setLayout(lay); finestra->show(); return app.exec();
}
@ -
"This is currently being discussed to be merged into Qt5":http://developer.qt.nokia.com/wiki/New_Signal_Slot_Syntax
There's a link to the repository with the code in that article and there's also a merge request for it with qtbase.And then stuff like this will be possible:
@
connect(sender, &Sender::valueChanged, [=](const QString &newValue) {
receiver->updateValue("senderValue", newValue);
} );
@
Or as is our example
@
QObject::connect(play, SIGNAL(clicked()), ={sli->setValue(150);});
@
THIS CODE IS NOT WORKING IN QT4, AND MIGHT NOT EVEN WORK IN QT5 AS THE SYNTAX IS SUBJECT TO CHANGE -
[quote author="loladiro" date="1313320494"]"This is currently being discussed to be merged into Qt5":http://developer.qt.nokia.com/wiki/New_Signal_Slot_Syntax
There's a link to the repository with the code in that article and there's also a merge request for it with qtbase.And then stuff like this will be possible:
@
connect(sender, &Sender::valueChanged, [=](const QString &newValue) {
receiver->updateValue("senderValue", newValue);
} );
@
Or as is our example
@
QObject::connect(play, SIGNAL(clicked()), ={sli->setValue(150);});
@
THIS CODE IS NOT WORKING IN QT4, AND MIGHT NOT EVEN WORK IN QT5 AS THE SYNTAX IS SUBJECT TO CHANGE[/quote]
Thanks, really impressive.