Qt Interface (control) related design

Source: Internet
Author: User

(Transferred from: http://blog.chinaunix.net/uid-25799257-id-600157.html)

Introduction recently in the database-related curriculum design, so take this opportunity, first familiar with some of the QT programming, while understanding some of the features of C + +. In fact, it is more important to organize the connection of related modules, how to plan projects, and so on. So just go through the process encountered problems and important some of the control slot and signal to introduce, later forgot to come back to see. Oh.

Here are some of the important functions and code that I used:One, the connection of the database1.Qsqldatabase TB = qsqldatabase::adddatabase ("Qmysql");//becomes the new default connect//tb.setconnectoptions ("client_ssl=1; Client_ignore_space=1 ");//Use SSL secure connectiontb.sethostname ("127.0.0.1");//Host nametb.setdatabasename ("chat");//Database name//tb.setport (3306);//Port numbertb.setusername ("root");//usernameTb.setpassword ("123");//Password2. Data avoidance cannot handle and insert Chinese character issuesCREATE DATABASE Chat character Set gbk;//you can insert Chinese or other methods to use chat;--View the character set of the database show variables like ' character \_set\_% '; Show variables like ' collation_% '; --Set database character encoding set names ' GBK '--so the Consle port view will not be garbled --drop table xxxx;--Modify Series--Add Head property ALTER TABLE Tb_user add He Ad varchar (20); --delete the data delete from Tb_userrs where friendID = "107865437"; --Update Tb_user set username= "Miao's World" where id= "642419907";  Second, the application of Chinese character processing (black code)Qtextcodec::setcodecfortr (Qtextcodec::codecforname ("GB18030"));//font handlingset the background color of the entire dialog box (black code)This->setautofillbackground (true);//1s stepQpalette palette;//2s StepPalette.setbrush (Qpalette::background, Qbrush (Qpixmap ("Image/login. JPG " ));This->setpalette (palette);//3s StepFour, application icon designicon Design, we have to put the. ico file in the ICO directory of the project, or other directories. Then create a file with the suffix. rc in the icon directory, as follows, Idi_icon1 icon discardable "Cool.ico", just like this in the program: This->setwindowicon (Qicon ("ico/ Cool.ico "));//Set the program ICO icon, note to do the settings in. Pro, OK. V. Implementation of the application Enter shortcut key event 1. Define a shortcut key in the header file qshortcut *key_enter_login;//shortcut key login2. define the corresponding slot private slots:void Enter_longin (); 3. In the constructor key_enter_login = new Qshortcut (qkeysequence (tr ("Return"), this);//Set Enter shortcut, but enter no, use Return. Connect (Key_enter_login, SIGNAL (activated ()), this, SLOT (Enter_longin () ));Vi. text linking to Web pages in the constructor : Qlabel *m_r_acount = new Qlabel (this); M_r_acount->settext (tr ("<a href=\" http://www.yafeilinux.com\ " > Registered account ")); Connect (M_r_acount, SIGNAL (linkactivated (QString)), this, SLOT (OPENURL (const QString))); slot ( do not forget the header file declaration slot) void Myfirstqq::openurl (const QString &register_url){Qdesktopservices::openurl (Qurl ("http://localhost:8080/chat/register.jsp")); }vi. settings for Qcombobox Qcombobox *m_a_choice = new Qcombobox (this); qstringlist c_strings;c_strings << "666666" << "642419907" << "767938089" << "107865437" << "110120119";completer = new Qcompleter (c_strings, this);//can be matched ohm_a_choice->clear ();M_a_choice->additems (c_strings);M_a_choice->setmaxvisibleitems (7);//sets the maximum display of the following items to be dragged with the scroll barm_a_choice->seteditable (true);M_a_choice->setcompleter (completer);Vi. Mouse over events in the header file: protected:void Mousemoveevent (qmouseevent *event);//mouse swipe in the constructor: //Set Mouse trace event this->setmousetracking (TRUE);//Tag Tracking Qlabel->setmousetracking (TRUE);//You can also set other controls in the implementation function: (for a small example) int x = event->x (), int y = event->y (), if (x > u_b_x && x < u_b_x+u_b_width && y > U_b _y && y < u_b_y+u_b_height) {U_background->setpixmap (Qpixmap ("image/skin3. JPG ")); } else if (x > u_button_x && x < u_button_x+u_button_width && y > u_button_y && y < U_b Utton_y+u_button_height) {textbox.show ();} else{U_background->setpixmap (Qpixmap ("Image/skin"). JPG ")); Textbox.close (); }Six, rewrite the label mouse click event Implementation (need to rewrite Qlabel, construct their own qlabel->mylabel)#ifndef clickedlabel_h_ #define CLICKEDLABEL_H_ #include <QtGui> class Clickedlabel:public Qlabel//Inherit Qlabel, Override event, implement click Tag Exit {q_object Public:clickedlabel (qwidget *parent = 0); int mylabelpressed; void Mousepressevent (Qmouseevent * e);//Add mouse response event void mousereleaseevent (Qmouseevent *e); Signals:void clicked ();//click Signal}; #endif/* Clickedlabel_h_ * / #include "ClickedLabel.h" Clickedlabel::clickedlabel (Qwidget *parent): Qlabel (parent) {mylabelpressed = 0;} void Cl Ickedlabel::mousepressevent (Qmouseevent * e) {mylabelpressed = 1;} void Clickedlabel::mousereleaseevent (QMouseEvent * e) {if (mylabelpressed) {emit clicked (); mylabelpressed = 0; } }When used, as long as the header file is included, and then use Clickedlabel to define one such label, then use the signal and slot as follows: Clickedlabel *s_exit;//exit the tag, so you can use the clicked () signal Connect (s_ Exit, SIGNAL (clicked ()), this, SLOT (closed ())); Rewrite Qlistwidget mouse Right-click popup menu event. (The implementation needs to override Qlistwidget)#ifndef listwidget_h_ #define Listwidget_h_ #include <QApplication> #include <QWidget> #include < qlistwidget> #include <QMenu> #include <QAction> #include "scaninfo.h" #include "Addfriend.h" class Listwidget:public qlistwidget {q_object public:explicit listwidget (qwidget *parent = 0);//explicit constructor can only be displayed call void con Textmenuevent (Qcontextmenuevent * event); Private:qaction *action;//Delete option qaction *scan_action;//view data qaction *add_friend;//add friend Qmenu *popmenu; }; #endif/* Listwidget_h_ * / #include "ListWidget.h" #include <QMessageBox> listwidget::listwidget (qwidget *parent):Qlistwidget (parent){action = new Qaction (Qicon ("Image/delete.jpg"), tr ("delete friend"), this);//Delete eventscan_action = new Qaction (Qicon ("Image/scan.jpg"), tr ("View material"), this);add_friend = new Qaction (Qicon ("Image/addfriend.jpg"), tr ("Add Friend"), this);Popmenu = new Qmenu (this);} void Listwidget::contextmenuevent (Qcontextmenuevent * Event) {If (This->itemat (Mapfromglobal (qcursor::p OS ()))! = NULL)//If any item is selected {Itemat (Mapfromglobal (qcursor::p OS ()))->setselected (true); }Else {popmenu->addaction (add_friend);popmenu->removeaction (action);popmenu->removeaction (scan_action);popmenu->exec (qcursor::p OS ()); }If (This->itemat (Mapfromglobal (qcursor::p OS ())) = NULL)//Add "delete" menu if there is Item {popmenu->addaction (action);popmenu->addaction (scan_action);popmenu->removeaction (add_friend);popmenu->exec (qcursor::p OS ());//The position where the menu appears is the current mouse position }}when used in the class you need (header file or constructor):Listwidget u_good_widget = new Listwidget (this); On line seven, similar to the small effect of the list of QQ, you can refer to1. Affirm the relevant slots of the QlistwidgetConnect (U_good_widget, SIGNAL (clicked (Qmodelindex)), this, SLOT (Good_stretch (Qmodelindex)));2. Groove Implementationvoid Userqq::good_stretch (Qmodelindex index) {if (U_flag) {U_item->seticon (Qicon (Qpixmap ("image/ Qlistwidgetitemup.png "))); } else{U_item->seticon (Qpixmap ("image/qlistwidgetitemdown.png"));} if (Index.row () ==0) {for (int j = 1; J < U_hide _count+1; J + +) {U_good_widget->setrowhidden (J, U_flag);} u_flag =!u_flag; Don't do this here. U_flag = false; The result is unexpected}}vii. Event definitions for qaction in QmenuQmenu *cmenu = null;//define and initialize right-click CMenu = new Qmenu (this);//Initialize right-click where to display qaction *entergrouptalk = cmenu->addaction (QIc On ("Image/grouptalk.jpg"), tr ("Enter group chat");//Add right-click menu display Item connect (entergrouptalk,signal (triggered ()), This,slot (on_ Actionenter_grouptalk_triggered ()));//make addrecode associated with other functionsviii. main forms, labels and some other designsthis->setfixedsize (This->width (), This->height ());//fixed size this->setwindowflags (Qt:: Framelesswindowhint);//Remove title bar Statement Qfont font ("ZYSong18030", 12);//Set Font This->setfont (fonts); Transparency This->setwindowopacity (0.8);//TagsQlabel S_acount_label = new Qlabel (this);S_acount_label->setfont (Qfont ("ZYSong18030", 8));//Font dotsS_acount_label->setgeometry (155, ten, a);Nine, the following is a small display effect, of course, many features did not realize, slowly improve it, mainly to summarize, whether good or badTen, (the above is not what very powerful, in fact, network programming should be very important.) My reference to achieve a group chat, but for TCP is not very familiar with, so slowly learn later, I still do not, so do not post, feel that they can do when the time again

Qt Interface (control) related design

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.