C++ GUI Programming with Qt3(系列三 试翻)

来源:互联网 发布:复印件生成器软件下载 编辑:程序博客网 时间:2024/05/02 15:25

C++ GUI Programming with Qt3(系列三 试翻)

        Qt widgets emit signals to indicate that a user action or a change of state has occurred. For instance , QpushButton emits a clicked() single when the user click the button。A singal can be connected to a function (called a slot in that context),so that when the singal is emitted the slots is automactically executed. In out example, we connect the button's clicked() singal to the Qapplication object's quit() slot. The SINGNAL() and SLOT() macros are part of syntax. They are explained in more detail in the next chapter.
        We will now build the application, we assume that you have created a directory called quit containing quit.cpp . Run qmake in the quit directory to generate the project file.then run it again to generate a makefile.
 Qmake -project
Qmke quit.pro
        Now build the application ,and run it,if you click Quit, or press Space(which presses the button),the application will terminate. 

        The next example demonstrates how to use signals and slots to synchronize two widgets. The application ask for the user's age, which the user can enter by manipulating either a spin box or slider.
 
 
        The application consists of tree widgets:a Qslider,a QspinBox,and a QHBox(horizontal layout box). The QHBox is application's main widget. The SpinBox and the Qslider,are rendered inside the QHBox.

001 #include <qapplication.h>
002 #include <qhbox.h>
003 #include <qslider.h>
004 #include <qspinbox.h>
005 int main(int argc, char *argv[])
006 {
007     QApplication app(argc, argv);
008     QHBox *hbox = new QHBox(0);
009     hbox->setCaption("Enter Your Age");
010     hbox->setMargin(6);
011     hbox->setSpacing(6);

012     QSpinBox *spinBox = new QSpinBox(hbox);
013     QSlider *slider = new QSlider(Qt::Horizontal, hbox);
014     spinBox->setRange(0, 130);
015     slider->setRange(0, 130);
016     QObject::connect(spinBox, SIGNAL(valueChanged(int)),
017     slider, SLOT(setValue(int)));
018     QObject::connect(slider, SIGNAL(valueChanged(int)),
019     spinBox, SLOT(setValue(int)));
020     spinBox->setValue(35);
021     app.setMainWidget(hbox);
022     hbox->show();
023     return app.exec();
024 }

Line 8 to 11 set up the QHBox. We call setCaption() to set the text displayed in the window’s title bar. Then we put some space(6 pixels) around and in between the child widgets.

Lines 12 and 13 create a QspinBox and a Qslider with the QHBox as the parent.

 

Even though we didn’t set position or size of any widget explicity, the QspinBox and Qslider appear nicely laid out side by side inside the QHBox. This is because QHBox automatically assigns reasonable positions and sizes to its children based on their needs. Qt  Private namy classes like QHBox to free us from the chore of hard coding screen position in our application.Line 14 and 15 set the valid range for the spin box and the slider.(we can safely assume that the user is at most 130 years old). The two connect() calls shown in lines 16 to 19 ensure that the spin box and the slider are synchronized so that they always show the same value. Whenever the value of one widget changes, its valueChanged( int) signal is emitted, and the setValue(int)slot of the other widget is called with the new value.
      Qt窗体控件用向外发送信号来表示用户的动作发生或本身状态的改变。例如,当用户单击程序中的Button该QpushButton对象就会发送一个clicked()信号。信号可以和某个特定的函数关联(向下文中成为反应槽)。关联的结果就是当一个信号被发送时和他关联的函数将会被调用。在本例中,我们把按钮的clicked()消息和Qapplication 对象的quit()反应函数关联起来。SINGNAL()和SLOT()宏是语法的一部分,在下一章将对他们进行进一步的描述。
        现在我们来编译连接该程序。我们认为你已经新建了一个名为quit的文件夹,并且该文件夹中有一个quit.cpp文件。在quit目录中运行qmake程序来产生该项目的项目文件。再次运行qmake程序来产生makefile。
Qmake -project
Qmake quit.pro 

        建立(译:用make命令)并运行该程序。当你单击或按下空格键是这个程序就会退出了(译:或叫终止运行)。
下来的例子将演示如何使用信号和反应槽来同步两个不同的窗体控件。该程序要求用户输入年龄。用户可以通过操作cpin box或slider两种方式来完成输入。
 

该程序有三个窗体控件:一个Qslider,一个QSPinBox和一个QHBox(水平排列控件)。QHBox是该应用程序的主窗体控件。QSPinBoxQslider被分布在他内部。




























8行到第11 新建并初始化QHBox。首先用seeCaption()来设置标题栏中所显示的文字。然后把他的子控件周围和两个子控件之间的空间(6 pixels

12行到13行新建QspinBoxQslider这两个窗体控件,并把他们设成QHBox的子控件。

虽然我们没有显式的去设置任何窗体控件的,但是QspinBoxQslider已经在QHBox中沿水平方向排列好了。那是因为QHBox已经根据他们的需要自动为他们(QHBox的子窗体控件)设置了合理的位置和大小。

Qt提供了很多类似QHBox这样额类,把我们从的为窗体的屏幕位置和大小写代码这样的杂事中解放出来。

1415行设置了spin控件和slider控件的范围(我们假设用户的年龄不会超过130岁)。16行到19行的两个connect()用来确保两个控件的同步,这样两个控件就会永远显示相同的值。两个控件中的其中任何一个控件的值发生改变是就会发送valueChangedint)信号,这样另外一个控件的setValueint)反应槽就会被调用,并且用新值做参数。

原创粉丝点击