新代码:
/****************************************************************
**
** Qt教程一 - 6
**
****************************************************************/
#include <QApplication>
#include <QPushButton>
#include <QSlider>
#include <QLcdNumber>
#include <QFont>
#include <QVBoxLayout>
#include <QGridLayout>
class LCDRange : public QWidget
{
public:
LCDRange( QWidget *parent=0, Qt::WindowFlags f = 0 );
};
LCDRange::LCDRange( QWidget *parent, Qt::WindowFlags f /*= 0*/ )
: QWidget( parent, f )
{
QLCDNumber *lcd = new QLCDNumber( 2, this );
QSlider * slider = new QSlider( Qt::Horizontal, this );
slider->setRange( 0, 99 );
slider->setValue( 0 );
connect( slider, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)) );
QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(lcd);
layout->addWidget(slider);
setLayout(layout);
}
class MyWidget : public QWidget
{
public:
MyWidget( QWidget *parent=0, Qt::WindowFlags f = 0 );
};
MyWidget::MyWidget( QWidget *parent, Qt::WindowFlags f /*= 0*/ )
: QWidget( parent, f )
{
QVBoxLayout* layout = new QVBoxLayout;
QPushButton *quit = new QPushButton( "Quit", this );
quit->setFont( QFont( "Times", 18, QFont::Bold ) );
quit->resize(300, 30);
connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) );
QGridLayout *grid = new QGridLayout( this );
for( int r = 0 ; r < 4 ; r++ )
{
for( int c = 0 ; c < 5 ; c++ )
{
grid->addWidget( new LCDRange( this ), r, c);
}
}
// 这儿是这么用吗?
layout->addWidget(quit, 1, Qt::AlignHCenter);
layout->addLayout(grid);
setLayout(layout);
}
int main( int argc, char **argv )
{
QApplication a( argc, argv );
MyWidget w;
w.resize(600, 100);
w.show();
return a.exec();
}
阅读(919) | 评论(0) | 转发(0) |