Qt thread learning a qthread example

Source: Internet
Author: User
A qthread example

// Threadtest. h
# Ifndef threadtest_h
# Define threadtest_h

# Include <qthread. h>
# Include "test. H"

Class mythread: Public qthread
{
Protected:
Virtual void run ();
};

Mythread;

Void test: newslot ()
{
Mythread. Start ();
}

# Endif

////////////////////////////////

// Threadtest. cpp

# Include <qdialog>

Void mythread: Run ()
{
For (INT I = 0; I <100; I ++)
{
Printf ("new value I is: % d", I );
// Qmessagebox: Information (this, TR ("info"), TR ("load DLL OK! "));
// Qmessagebox: Information (this, TR ("empty search"), "click find .");
// Test m;
// M. Show ();
Qdebug ("mhf ");
}
/*
Qdialog * dialog = new qdialog (0, "popup", false );
Dialog-> setcaption ("A qdialog window ");
Dialog-> setminimumsize (200,80 );
Dialog-> setmaximumsize (200,80 );
Dialog-> show ();
*/
}

//////////////////////////////////////// /////////////////////

// Test. cpp

# Include "test. H"

# Include <qvariant. h>
# Include <qlineedit. h>
# Include <qpushbutton. h>
# Include <qtextedit. h>
# Include <qlayout. h>
# Include <qtooltip. h>
# Include <qwhatsthis. h>
# Include <qimage. h>
# Include <qpixmap. h>
# Include <qvboxlayout>

/*
* Constructs a test as a child of 'parent', with
* Name 'name' and Widget flags set to 'F '.
*/
Test: Test (qwidget * parent): qwidget (parent = 0)
{
This-> setwindowtitle ("W ");
This-> resize( 250, 50 );

Textedit1 = new qtextedit ("textedit1 ");
Textedit1-> setgeometry (qrect (10, 60,570,291 ));

Lineedit1 = new qlineedit ("lineedit1 ");
Lineedit1-> setgeometry (qrect (10,360,571, 31 ));

Pushbutton2 = new qpushbutton ("pushbutton2 ");
Pushbutton2-> setgeometry (qrect (380,400,201, 41 ));

Pushbutton1 = new qpushbutton ("pushbutton1 ");
Pushbutton1-> setgeometry (qrect (10, 10,261, 41 ));
// Languagechange ();
Resize (qsize (600,480). expandedto (minimumsizehint ()));

// Signals and slots connections
Connect (pushbutton1, signal (clicked (), this, slot (newslot ()));
Connect (pushbutton2, signal (clicked (), this, slot (languagechange ()));
// Add
Qvboxlayout * layout = new qvboxlayout;
Layout-> addwidget (textedit1 );
Layout-> addwidget (lineedit1 );
Layout-> addwidget (pushbutton1 );
Layout-> addwidget (pushbutton2 );

This-> setlayout (layout );
This-> resize( 350,200 );

}

/*
* Destroys the object and frees any allocated resources
*/
Test ::~ Test ()
{
// No need to delete child widgets, QT does it all for us
}

/*
* Sets the strings of the subwidgets using the current
* Language.
*/
Void test: languagechange ()
{
This-> setwindowtitle ("M ");
// Printf ("new value I is ");
Pushbutton2-> settext (TR ("pushbutton2 "));
Pushbutton1-> settext (TR ("pushbutton1 "));
}

///////////////////////////////////////

// Test. h
# Ifndef test_h
# Define test_h

# Include <qvariant. h>
# Include <qwidget. h>

Class qvboxlayout;
Class qhboxlayout;
Class qgridlayout;
Class qlineedit;
Class qpushbutton;
Class qtextedit;

Class test: Public qwidget
{
Q_object

Public:
Test (qwidget * parent = 0 );
~ Test ();

Qtextedit * textedit1;
Qlineedit * lineedit1;
Qpushbutton * pushbutton2;
Qpushbutton * pushbutton1;

Public slots:
Virtual void newslot ();

Protected:

Protected slots:
Virtual void languagechange ();
};

# Endif // test_h

//////////////////////////////////////

Main. cpp

# Include <qapplication. h>
# Include "test. H"

Int main (INT argc, char ** argv)
{
Qapplication A (argc, argv );
Test W;
W. Show ();
A. Connect (& A, signal (lastwindowclosed (), & A, slot (quit ()));
Return a.exe C ();
}

/////////////

Debug and run

GDB *. exe

Run step: click the button to view the output debugging information.

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.