You are not logged in.

graciano

Beginner

  • "graciano" is male
  • "graciano" started this thread

Posts: 16

Location: Portugal

  • Send private message

1

Thursday, April 24th 2008, 3:04pm

How to change multiple sliders with the same signal

Hi
This is only my third exercice with QT.
What must i do so that all 3 sliders are reseted to 0 when i press the "reset" button?


Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <QApplication>
#include <QHBoxLayout>
#include <QSlider>
#include <QSpinBox>
#include <QPushButton>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QWidget *window = new QWidget;
    window->setWindowTitle("Enter Your Age");

    QSpinBox *spinBox = new QSpinBox;
    QSlider *slider = new QSlider(Qt::Horizontal);
    QPushButton *botao = new QPushButton("Lixo");
    QPushButton *sair = new QPushButton("Sair");
    spinBox->setRange(0, 130);
    slider->setRange(0, 130);

	QObject::connect(	sair, SIGNAL(clicked()),
						&app, SLOT(quit()));
    QObject::connect(spinBox, SIGNAL(valueChanged(int)),
                     slider, SLOT(setValue(int)));
    QObject::connect(slider, SIGNAL(valueChanged(int)),
                     spinBox, SLOT(setValue(int)));
    slider->setValue(40);
    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(spinBox);
    layout->addWidget(slider);
    layout->addWidget(botao);
    layout->addWidget(sair);
    window->setLayout(layout);

    window->show();

    return app.exec();
}


Must be easy but i'm just begining.
Thanks
Graciano Torrão

2

Saturday, May 3rd 2008, 2:28am

RE: How to change multiple sliders with the same signal

in the reset::onclicked , you need to tell the sliders to reset to 0.

graciano

Beginner

  • "graciano" is male
  • "graciano" started this thread

Posts: 16

Location: Portugal

  • Send private message

3

Saturday, May 3rd 2008, 7:18pm

RE: How to change multiple sliders with the same signal

ops ... only now i realised that i posted the wrong code :rolleyes:
I was talking about this situation.

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <QApplication>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QSpinBox>
#include <QSlider>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    
	QWidget *janela = new QWidget;
	janela->setWindowTitle("Modelo RGB!");
	
    QPushButton *botaosair = new QPushButton("Sair");
    QPushButton *botaoreset = new QPushButton("Reset");
    
    QLabel *textoRed = new QLabel("Red");
    QLabel *textoGreen = new QLabel("Green");
    QLabel *textoBlue = new QLabel("Blue");
    
    QSpinBox *spinBoxRed = new QSpinBox;
    QSpinBox *spinBoxGreen = new QSpinBox;
    QSpinBox *spinBoxBlue = new QSpinBox;
    
    spinBoxRed->setRange(0,255);
    spinBoxGreen->setRange(0,255);
    spinBoxBlue->setRange(0,255);
    
    QSlider *sliderRed = new QSlider(Qt::Horizontal);
    QSlider *sliderGreen = new QSlider(Qt::Horizontal);
    QSlider *sliderBlue = new QSlider(Qt::Horizontal);
    
    sliderRed->setRange(0,255);
    sliderGreen->setRange(0,255);
    sliderBlue->setRange(0,255);
   
    
    //Signals and slots
    QObject::connect(	spinBoxRed, 	SIGNAL(valueChanged(int)),
    					sliderRed,		SLOT(setValue(int)));
    QObject::connect(	sliderRed, 		SIGNAL(valueChanged(int)),
    					spinBoxRed,		SLOT(setValue(int)));
    QObject::connect(	spinBoxGreen,	SIGNAL(valueChanged(int)),
    					sliderGreen,	SLOT(setValue(int)));
    QObject::connect(	sliderGreen, 	SIGNAL(valueChanged(int)),
    					spinBoxGreen,	SLOT(setValue(int)));
    QObject::connect(	spinBoxBlue, 	SIGNAL(valueChanged(int)),
    					sliderBlue,		SLOT(setValue(int)));
    QObject::connect(	sliderBlue, 	SIGNAL(valueChanged(int)),
    					spinBoxBlue,	SLOT(setValue(int)));
    QObject::connect(	botaosair, 		SIGNAL(clicked()),
    					&app,			SLOT(quit()));
    QObject::connect(	botaoreset, 	SIGNAL(clicked()),
    					sliderRed,		SLOT(setValue(0)));

 
    //layouts
    QVBoxLayout *cores = new QVBoxLayout;
    cores->addWidget(textoRed);
    cores->addWidget(textoGreen);
    cores->addWidget(textoBlue);
    
    QVBoxLayout *spinbox = new QVBoxLayout;
    spinbox->addWidget(spinBoxRed);
    spinbox->addWidget(spinBoxGreen);
    spinbox->addWidget(spinBoxBlue);
    
    QVBoxLayout *slider = new QVBoxLayout;
    slider->addWidget(sliderRed);
    slider->addWidget(sliderGreen);
    slider->addWidget(sliderBlue);

 	QHBoxLayout *layoutRGB = new QHBoxLayout;
 	layoutRGB->addItem(cores);
 	layoutRGB->addItem(spinbox);
 	layoutRGB->addItem(slider);
 	
 	QHBoxLayout *layoutControlo = new QHBoxLayout;
 	layoutControlo->addWidget(botaoreset);
 	layoutControlo->addWidget(botaosair);
 	
 	QVBoxLayout *layoutjanela = new QVBoxLayout;
 	layoutjanela->addItem(layoutRGB);
 	layoutjanela->addItem(layoutControlo);
 	 
 	    
    janela->setLayout(layoutjanela);
    
    janela->show();
    return app.exec();
}


So the problem must be in this line?
QObject::connect( botaoreset, SIGNAL(clicked()),
sliderRed, SLOT(setValue(0)));
And then i will have to repeat it for green an blue sliders?
Thanks and sorry for the mess.
Graciano Torrão

Marco38

Beginner

  • "Marco38" is male

Posts: 3

Location: FRANCE

Occupation: Student

  • Send private message

4

Thursday, May 22nd 2008, 11:48pm

My solution to your problem...

Hello Graciano,

I'd say that you're right, the problem is in this line :

QObject::connect( botaoreset, SIGNAL(clicked()), sliderRed, SLOT(setValue(0)));

This ain't no good idea to write "setValue(0)" in the SLOT macro, where one should only write function names, with parameters types, not actual values. But if you replace "setValue(0)" with "setValue(int)", the program doesn't know which value you want to set the slider to, so you must specify it somewhere else. You could write a function named, say, "resetSliders ( )", that would do the job, but another problem is that a function defined outside of your 'main' function cannot access the sliders declared inside the 'main' function. For all these reasons, the solution I advise you to implement is the following: write a small class inheriting QWidget, or QDialog, declare the three sliders (and all other widgets) as members of this class, and use this class as the application's main window. The function "resetSliders ( )" should then be a member function of this class, so that it can access the sliders. It is especially important to place the declaration of the class in a separate header file (janela.h or mainwindow.h), otherwise you will experience rather weird messages "undefined reference to vtable for...". And it is a good practice, although not mandatory, to place the definition of your classes in a separate source file for each class (janela.cpp or mainwindow.cpp, in this example), not in the file that contains the 'main' function.

Also, to place layouts into another layout, you should use 'addLayout ( )', not 'addItem ( )'. This yields better results, with widgets more nicely laid out in the window.

mainwindow.h :

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QDialog>

class QPushButton;
class QLabel;
class QSpinBox;
class QSlider;

class QGridLayout;
class QVBoxLayout;
class QHBoxLayout;

class MainWindow : public QWidget
{
  Q_OBJECT

  public :

  MainWindow ( QWidget* parent = 0 );

  private slots :

  void resetSliders ( );

  private :

  QPushButton *botaoReset;
  QPushButton *botaoSair;

  QLabel *textoRed;
  QLabel *textoGreen;
  QLabel *textoBlue;

  QSpinBox *spinBoxRed;
  QSpinBox *spinBoxGreen;
  QSpinBox *spinBoxBlue;

  QSlider *sliderRed;
  QSlider *sliderGreen;
  QSlider *sliderBlue;
};

#endif


mainwindow.cpp :

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <QtGui>

#include "mainwindow.h"

MainWindow::MainWindow ( QWidget *parent ) : QWidget ( parent )
{
  botaoReset = new QPushButton ( "Reset" );
  botaoSair  = new QPushButton ( "Sair"  );

  textoRed   = new QLabel ( "Red"   );
  textoGreen = new QLabel ( "Green" );
  textoBlue  = new QLabel ( "Blue"  );

  spinBoxRed   = new QSpinBox;
  spinBoxGreen = new QSpinBox;
  spinBoxBlue  = new QSpinBox;

  spinBoxRed   -> setRange ( 0, 255 );
  spinBoxGreen -> setRange ( 0, 255 );
  spinBoxBlue  -> setRange ( 0, 255 );

  sliderRed   = new QSlider ( Qt::Horizontal );
  sliderGreen = new QSlider ( Qt::Horizontal );
  sliderBlue  = new QSlider ( Qt::Horizontal );

  sliderRed   -> setRange ( 0, 255 );
  sliderGreen -> setRange ( 0, 255 );
  sliderBlue  -> setRange ( 0, 255 );

  // Signals and slots :

  connect ( spinBoxRed,   SIGNAL ( valueChanged ( int )), sliderRed,SLOT ( setValue ( int )));
  connect ( sliderRed,SIGNAL ( valueChanged ( int )), spinBoxRed,   SLOT ( setValue ( int )));
  connect ( spinBoxGreen, SIGNAL ( valueChanged ( int )), sliderGreen,  SLOT ( setValue ( int )));
  connect ( sliderGreen,  SIGNAL ( valueChanged ( int )), spinBoxGreen, SLOT ( setValue ( int )));
  connect ( spinBoxBlue,  SIGNAL ( valueChanged ( int )), sliderBlue,  SLOT ( setValue ( int )));
  connect ( sliderBlue,   SIGNAL ( valueChanged ( int )), spinBoxBlue,  SLOT ( setValue ( int )));

  connect ( botaoSair,  SIGNAL ( clicked ( )), this, SLOT ( close ( )));
  connect ( botaoReset, SIGNAL ( clicked ( )), this, SLOT ( resetSliders ( )));

  // Layouts :

  QGridLayout *topLayout = new QGridLayout;

  topLayout -> addWidget ( textoRed, 0, 0, 1, 1 );
  topLayout -> addWidget ( spinBoxRed,   0, 1, 1, 1 );
  topLayout -> addWidget ( sliderRed,0, 2, 1, 1 );

  topLayout -> addWidget ( textoGreen,   1, 0, 1, 1 );
  topLayout -> addWidget ( spinBoxGreen, 1, 1, 1, 1 );
  topLayout -> addWidget ( sliderGreen,  1, 2, 1, 1 );

  topLayout -> addWidget ( textoBlue,2, 0, 1, 1 );
  topLayout -> addWidget ( spinBoxBlue,  2, 1, 1, 1 );
  topLayout -> addWidget ( sliderBlue,   2, 2, 1, 1 );

  QHBoxLayout *layoutControlo = new QHBoxLayout;

  layoutControlo -> addWidget ( botaoReset );
  layoutControlo -> addWidget ( botaoSair  );

  QVBoxLayout *layoutJanela = new QVBoxLayout;

  layoutJanela -> addLayout ( topLayout  );
  layoutJanela -> addLayout ( layoutControlo );

  setLayout ( layoutJanela );
}

void MainWindow::resetSliders ( )
{
  sliderRed   -> setValue ( 0 );
  sliderGreen -> setValue ( 0 );
  sliderBlue  -> setValue ( 0 );
}


main.cpp :

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <QApplication>

#include "mainwindow.h"

int main ( int argc, char *argv [] )
{
  QApplication app ( argc, argv );

  MainWindow *janela = new MainWindow;

  janela -> setWindowTitle ( "Modelo RGB!" );

  janela -> show ( );

  return app.exec ( );
}


qmake -project
qmake
make


And there you go !

I hope this helped you.

Marco38, from Grenoble ( France ) ;)

graciano

Beginner

  • "graciano" is male
  • "graciano" started this thread

Posts: 16

Location: Portugal

  • Send private message

5

Friday, May 23rd 2008, 2:55pm

Thanks A LOT, ou melhor, obrigadão :D
Graciano Torrão