迁移《Qt教程一 —— 第六章:组装丰富的积木!》到Qt 4.3.0
新代码:
/*
***************************************************************
**
** 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();
}
**
** 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();
}
不过有问题,达不到如下效果,谁帮我看看 ?就是quit按钮跟grid的位置不和谐
2007-7-22:
这个教程居然就是qt自带的tutorial t6,晕倒
这样子改就可以了:
/*
***************************************************************
**
** 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 );
QSlider * slider = new QSlider( Qt::Horizontal);
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 " );
quit -> setFont( QFont( " Times " , 18 , QFont::Bold ) );
connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) );
QGridLayout * grid = new QGridLayout;
for ( int r = 0 ; r < 4 ; r ++ )
{
for ( int c = 0 ; c < 5 ; c ++ )
{
grid -> addWidget( new LCDRange( this ), r, c);
}
}
layout -> addWidget(quit);
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();
}
**
** 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 );
QSlider * slider = new QSlider( Qt::Horizontal);
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 " );
quit -> setFont( QFont( " Times " , 18 , QFont::Bold ) );
connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) );
QGridLayout * grid = new QGridLayout;
for ( int r = 0 ; r < 4 ; r ++ )
{
for ( int c = 0 ; c < 5 ; c ++ )
{
grid -> addWidget( new LCDRange( this ), r, c);
}
}
layout -> addWidget(quit);
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();
}