View Full Version : how to make a button resets a slider to 0?
lunar
20th May 2009, 10:21
hello ^_^
i have been programming in QT since yesterday and i got a question how do i reset slider to 0?
void setValue ( int )
Changing the value also changes the sliderPosition.
but it didnt work :confused:
connect(resetButton, SIGNAL(clicked() ), slider, SLOT(setValue(0) ) );
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QLCDNumber>
#include <QSlider>
#include <QVBoxLayout>
#include <iostream>
class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent);
~MyWidget() { std::cout << "abc" << std::endl; }
};
MyWidget::MyWidget(QWidget *parent = 0) : QWidget(parent)
{
setFixedSize(320, 200);
QPushButton *resetButton = new QPushButton("Reset");
QLCDNumber *lcd = new QLCDNumber(2);
lcd->setSegmentStyle(QLCDNumber::Filled);
QSlider *slider = new QSlider(Qt::Horizontal);
slider->setValue(0);
slider->setRange(0, 99);
connect(resetButton, SIGNAL(clicked() ), slider, SLOT(setValue(0) ) );
connect(slider, SIGNAL(valueChanged(int) ), lcd, SLOT(display(int) ) );
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(resetButton);
layout->addWidget(lcd);
layout->addWidget(slider);
setLayout(layout);
}
int main(int argc, char *argv[] )
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
app.exec();
}
wagmare
20th May 2009, 10:36
connect(resetButton, SIGNAL(clicked() ), slider, SLOT(setValue(0) ) );
connect(slider, SIGNAL(valueChanged(int) ), lcd, SLOT(display(int) ) );
u cannot connect a void SIGNAL to int SLOT
clicked() ->setValue(0) connection is not possible .. but reverse is possible ..
so create a private slot
private slot:
void sliderValue();
connect(resetButton, SIGNAL(clicked()), this, SLOT(sliderValue()));
MyWidget::sliderValue()
{
slider->setValue(0);
}
lunar
20th May 2009, 13:33
hey thx for help but iam running a little error:
In member function `void MyWidget::SetSliderValue()':|
error: `slider' was not declared in this scope|
warning: unused variable 'slider'|
||=== Build finished: 1 errors, 1 warnings ===|
class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent);
~MyWidget() { std::cout << "abc" << std::endl; }
private slots:
void SetSliderValue() { slider->setValue(0); }
};
connect(resetButton, SIGNAL(clicked() ), this, SLOT(SetSliderValue() ) );
wagmare
20th May 2009, 13:58
hey thx for help but iam running a little error:
connect(resetButton, SIGNAL(clicked() ), this, SLOT(SetSliderValue() ) );
declare slider in private (global)
private :
QSlider *slider;
use it like this
class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent);
~MyWidget() { std::cout << "abc" << std::endl; }
private slots:
void SetSliderValue() { slider->setValue(0); }
private:
QSlider *slider;
};
otherwise u cant use it in separate function of the class ...
lunar
20th May 2009, 14:33
damn :mad: didnt work
http://img34.imageshack.us/img34/5822/asdasdasdn.th.jpg (http://img34.imageshack.us/my.php?image=asdasdasdn.jpg)
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QLCDNumber>
#include <QSlider>
#include <QVBoxLayout>
#include <iostream>
class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent);
~MyWidget() { std::cout << "abc" << std::endl; }
private:
QSlider *slider;
private slots:
void SetSliderValue() { slider->setValue(0); }
};
MyWidget::MyWidget(QWidget *parent = 0) : QWidget(parent)
{
setFixedSize(320, 200);
QPushButton *resetButton = new QPushButton("Reset");
QLCDNumber *lcd = new QLCDNumber(2);
lcd->setSegmentStyle(QLCDNumber::Filled);
QSlider *slider = new QSlider(Qt::Horizontal);
slider->setValue(0);
slider->setRange(0, 99);
connect(resetButton, SIGNAL(clicked() ), this, SLOT(SetSliderValue() ) );
connect(slider, SIGNAL(valueChanged(int) ), lcd, SLOT(display(int) ) );
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(resetButton);
layout->addWidget(lcd);
layout->addWidget(slider);
setLayout(layout);
}
int main(int argc, char *argv[] )
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
app.exec();
}
wagmare
20th May 2009, 14:38
relax ................ u are new ...
change this ..
QSlider *slider = new QSlider(Qt::Horizontal);
to
slider = new QSlider(Qt::Horizontal);
because u have already defined it in private:
private:
QSlider *slider;
wagmare
20th May 2009, 14:47
i think u need the macro Q_OBJECT to connect ... it is giving error n connect .. how stupid i am not to mention it earlier ..
lunar
20th May 2009, 14:52
hmm didnt work i think something is wrong with the button :)
i have also added #include <QObject> and Q_OBJECT yesterday but didnt work
QObject: No such file or directory|
||=== Build finished: 1 errors, 0 warnings ===|
what about " emit "
void Counter::setValue(int value)
{
if (value != m_value) {
m_value = value;
emit valueChanged(value);
}
}
wagmare
20th May 2009, 15:01
see the code i attached with this thread ... i cant set the setValue(0) to slider .... its giving seg fault ...
wagmare
20th May 2009, 15:10
yipeeee i completed this one .... its working .. your logic is right .. use this attachment
lunar
20th May 2009, 15:23
i believe it will work but:
|7|QObject: No such file or directory|
||=== Build finished: 1 errors, 0 warnings ===| :eek:
looks like its gonna be more complicated than WinApi :D:D
do u know what mistake does show this error? :mad:
Lykurg
20th May 2009, 21:09
|7|QObject: No such file or directory|
||=== Build finished: 1 errors, 0 warnings ===|
May you have to explicit add a #include <QObject> in the header file or more likely clean your directory before you build the new files.
wagmare
21st May 2009, 05:21
which os u are using .. i compiled it in linux .... ok make clean then give make ...
else qmake again the .pro file ...
did u see what are the error u done .. ? in the code ...
vonCZ
21st May 2009, 07:43
FYI- it worked for me, too. WinXP, Visual Studio 2005 Command Prompt:
C:\prompt> qmake
C:\prompt> nmake release
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.