Qt learning 4

Source: Internet
Author: User

You can use a class to implement a simple layout.

# Include <qapplication. h> # include <qmainwindow. h> # include <qpushbutton. h> # include <qslider. h> # include <qvboxlayout> # include <qhboxlayout> # include <qwidget. h ># include <qlcdnumber> class mywidget: Public qwidget {public: mywidget (qwidget * parent = 0, const char * name = 0) ;}; mywidget :: mywidget (qwidget * parent, const char * Name): qwidget (parent) {// create a slider for a single LCD and associate qlcdnumber * LCD = new qlcdnumber (2 ); qslider * slider = new qslider (QT: horizontal, this); slider-> setrange (0, 99); slider-> setvalue (0 ); // use the signal/slot mechanism to connect the valuechanged () signal of the slider with the display () slot of the LCD number connect (slider, signal (valuechanged (INT), LCD, slot (Display (INT); qvboxlayout * layout = new qvboxlayout; Layout-> addwidget (LCD); Layout-> addwidget (slider); setlayout (layout );} class mywidget1: Public qwidget {public: mywidget1 (qwidget * parent = 0);}; mywidget1: mywidget1 (qwidget * parent): qwidget (parent) {// horizontal layout, create a row of four mywidget objects qhboxlayout * layout = new qhboxlayout; mywidget * m [4]; for (Int J = 0; j <4; j ++) {M [J] = new mywidget;} For (INT I = 0; I <4; I ++) {Layout-> addwidget (M [I]);} setlayout (layout);} class mywidget2: Public qwidget {public: mywidget2 (qwidget * parent = 0);}; mywidget2: mywidget2 (qwidget * parent): qwidget (parent) {// This class is vertical layout. Create a row of qvboxlayout * layout = new qvboxlayout; mywidget1 * m [4]; qpushbutton * bn = new qpushbutton ("quit ", this); qobject: connect (bn, signal (clicked (), qapp, slot (quit (); Layout-> addwidget (BN ); for (Int J = 0; j <4; j ++) {M [J] = new mywidget1;} For (INT I = 0; I <4; I ++) {Layout-> addwidget (M [I]); setlayout (layout) ;}} int main (INT argc, char ** argv) {qapplication A (argc, argv ); // Class Object mywidget2 W; W. show (); Return a.exe C ();}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.