头文件
:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QLineEdit>
#include <QTextEdit>
#include <QComboBox>
#include <QTimeEdit>
#include <QDial>
#include <QSlider>
#include <QScrollBar>
#include <QLabel>
#include <QLCDNumber>
#include <QProgressBar>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
QLineEdit *LineEdit;
QTextEdit *TextEdit;
QComboBox *ComboBox;
QTimeEdit *TimeEdit;
QDial * Dial;
QSlider *Slider;
QScrollBar *ScrollBar;
QLabel *Label1,*Label2;
QLCDNumber *LCDNumber;
QProgressBar *ProgressBar;
};
#endif // WIDGET_H
Widget.cpp,类的实现
#include "widget.h"
#include "ui_widget.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
LineEdit = new QLineEdit;
TextEdit = new QTextEdit;
ComboBox = new QComboBox;
ComboBox->addItem("wzfdashuaibi");
ComboBox->addItem("woshinibaba");
TimeEdit = new QTimeEdit;
Dial = new QDial;
Dial->setBaseSize(1000,1000);
Slider =new QSlider;
Slider->setOrientation(Qt::Horizontal);
ScrollBar = new QScrollBar;
ScrollBar->setOrientation(Qt::Horizontal);
QVBoxLayout *vbox0 = new QVBoxLayout;
vbox0->addWidget(LineEdit);
vbox0->addWidget(TextEdit);
vbox0->addWidget(ComboBox);
vbox0->addWidget(TimeEdit);
vbox0->addWidget(Dial);
vbox0->addWidget(Slider);
vbox0->addWidget(ScrollBar);
Label1 = new QLabel("HELLO WORLD!");
Label2 = new QLabel;
QPixmap pix("1.png");
Label2->setFixedSize(100,120);
Label2->setScaledContents(true);
Label2->setPixmap(pix);
LCDNumber = new QLCDNumber;
ProgressBar = new QProgressBar;
QVBoxLayout *vbox1 = new QVBoxLayout;
vbox1->addWidget(Label1);
vbox1->addWidget(Label2);
vbox1->addWidget(LCDNumber);
vbox1->addWidget(ProgressBar);
QHBoxLayout *Hbox = new QHBoxLayout;
Hbox->addLayout(vbox0);
Hbox->addLayout(vbox1);
this->setLayout(Hbox);
//信号与槽
connect(Dial,SIGNAL(valueChanged(int)), LCDNumber,SLOT(display(int)));
connect(Dial,SIGNAL(valueChanged(int)), Slider,SLOT(setValue(int)));
connect(Dial,SIGNAL(valueChanged(int)), ScrollBar,SLOT(setValue(int)));
}
Widget::~Widget()
{
delete ui;
}
mian
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
效果: