C ++ Gui Programming with Qt3 (Series Three Test)

xiaoxiao2021-03-06  73

C Gui Programming with Qt3 (Series Three Test)

. 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 002.. #include 003 #include 004 #include 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 (016 Qobject :: Connect (SpinBox, Signal (ValueChanged int)), 017 Slider, Slot (SetValue (int))); 018 QObject :: Connect (Slider, Signal (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 did not 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/h value. QT Form Control Forward signaling signals to represent the user's action or changes in its own state. For example, when the user clicks the Button object in the program, the QPushButton object will send a CLICKED () signal. Signals can be associated with a particular function (in the next article). The result of the association is that when a signal is sent, the function he associated will be called. In this example, we associate the button's Clicked () message and the Quit () response function of the QAPPlication object. Singnal () and slot () macro are part of the grammar, which will be further described in the next chapter. Now let's compile the connection to the program. We think you have created a new folder called quit, and there is a quit.cpp file in this folder. Run the QMAKE program in the Quit directory to generate project files for the project. Run the QMAKE program again to generate makefile. QMake -Project Qmake Quit.Pro is established (translated: with the make command) and run the program. When you click or press the space bar, you will exit (translate: or terminate running). The examples will demonstrate how to synchronize two different form controls using signals and reaction tanks. The program requires the user to enter age. Users can perform input by operating CPIN BOX or SLIDER. The program has three form controls: a QSLider, a QspinBox and a QHBOX (horizontal arrangement). QHBOX is the main form control of the application. QspinBox and QSLIDER are distributed interior. Chapter 8 to 11th lines new and initialize QHBOX. First use seecaption () to set the text shown in the title bar. Then, the space (6 Pixels) between his sub-control and the space (6 Pixels) between the two sub-controls, 12th line, and set two form controls to the QSPinbox and Qslider and set them into QHBOX sub-controls.

转载请注明原文地址:https://www.9cbs.com/read-92915.html

New Post(0)