PDA

View Full Version : What's wrong with my code?



Dave2011
9th August 2011, 16:05
Code:
**************************
lcdrange.h:


#ifndef LCDRANGE_H
#define LCDRANGE_H

#endif // LCDRANGE_H
#include<QWidget.h>
#include<QSlider.h>
#include<QLcdnumber.h>

class Lcdrange: public QWidget
{
Q_OBJECT
public:
Lcdrange();
int value();
private:
QSlider* slider;
QLCDNumber* lcd;
public slots:
void setValue(int);
signals:
void valueChanged(int);
};

***************************
lcdrange.cpp:



#include"lcdrange.h"

Lcdrange::Lcdrange()
{
slider=new QSlider(Qt::Horizontal,this);
slider->setRange(0,99);
slider->setGeometry(0,0,200,50);

lcd=new QLCDNumber(2,this);
lcd->setGeometry(0,50,200,50);

connect(slider,SIGNAL(valueChanged(int)),lcd,SLOT( display(int)));
connect(slider,SIGNAL(valueChanged(int)),SIGNAL(va lueChanged(int)));
}

int Lcdrange::value()
{
return slider->value();
}
void Lcdrange::setValue(int value)
{
return slider->setValue(value);
}



************************************
main.cpp:


#include<qapplication.h>
#include<qpushbutton.h>
#include"lcdrange.h"

class MyWindow: public Lcdrange
{
public:
MyWindow();
private:
QPushButton* button;
};

MyWindow::MyWindow()
{
setGeometry(200,150,200,250);

button=new QPushButton("Quit!",this);
button->setGeometry(100,210,90,40);

Lcdrange* range=new Lcdrange;
range->setGeometry(0,0,200,100);

Lcdrange* ranges=new Lcdrange;
ranges->setGeometry(0,100,200,100);

connect(range,SIGNAL(valueChanged(int)),ranges,SLO T(setValue(int)));
connect(button,SIGNAL(clicked()),qApp,SLOT(quit()) );
}

int main(int argc,char** argv)
{
QApplication a(argc,argv);

MyWindow mw;

mw.show();
return a.exec();
}

mvuori
9th August 2011, 16:18
#endif // LCDRANGE_H
should be at the end of the .h file, preventing including it if it has already been included.

And MyWindow class needs to be defined in a .h file, so it can include the Q_OBJECT macro, which it needs for signals & slots to work.

Other than that, there must be a reason to think there is something wrong. Usually the reason for that is that the code does not work. If that is the case, you should describe what it does and what it does wrong.

Lykurg
9th August 2011, 16:19
Should we search or would you be so kind to tell us, what error do you get? Also please note the [code] tags.

Dave2011
12th August 2011, 12:18
I am a beginner here,I am so sorry I didn't know how to note [code] tags.I designed the MyWindow class have two lcdnumber and two slider,but it just showed one lcdnumber and one slider.I don't know what'wrong with it.

wysota
12th August 2011, 22:35
Please learn to use layouts in your programs.