Common QT skills

Source: Internet
Author: User
Tags xsl

This article was told by a Web user a few days ago. After reading it, I saved it. Check again today
I think it is necessary to share the article with my friends who are learning QT, because the useful QT resources on the Internet are true.
Good little.
1. If you determine whether the form can be closed before it is closed
A: implement the closeevent () function of the form again and add the judgment operation.
Quote:
Void mainwindow: closeevent (qcloseevent * event)
{
If (maybesave ())
{
Writesettings ();
Event-> Accept ();
}
Else
{
Event-> ignore ();
}
}
2. How to open and save a File Dialog
A: Use qfiledialog.
Quote:
Qstring filename = qfiledialog: getopenfilename (this );
If (! Filename. isempty ())
{
LoadFile (filename );
}
Quote:
Qstring filename = qfiledialog: getsavefilename (this );
If (filename. isempty ())
{
Return false;
}
3. If actions is created (these actions can be used in the menu and toolbar)
A:
Quote:
Newact = new qaction (qicon (":/images/new.png"), TR ("& New"), this );
Newact-> setshortcut (TR ("Ctrl + N "));
Newact-> setstatustip (TR ("Create a new file "));
Connect (newact, signal (triggered (), this, slot (newfile ()));
Openact = new qaction (qicon (":/images/open.png"), TR ("& open..."), this );
Openact-> setshortcut (TR ("Ctrl + O "));
Openact-> setstatustip (TR ("open an existing file "));
Connect (openact, signal (triggered (), this, slot (open ()));
Saveact = new qaction (qicon (":/images/save.png"), TR ("& Save"), this );
Saveact-> setshortcut (TR ("Ctrl + S "));
Saveact-> setstatustip (TR ("Save the document to disk "));
Connect (saveact, signal (triggered (), this, slot (save ()));
Saveasact = new qaction (TR ("Save & as..."), this );
Saveasact-> setstatustip (TR ("Save the document under a new name "));
Connect (saveasact, signal (triggered (), this, slot (saveas ()));
Exitact = new qaction (TR ("E & xit"), this );
Exitact-> setshortcut (TR ("Ctrl + q "));
Exitact-> setstatustip (TR ("exit the application "));
Connect (exitact, signal (triggered (), this, slot (close ()));
Cutact = new qaction (qicon (":/images/cut.png"), TR ("Cu & T"), this );
Cutact-> setshortcut (TR ("Ctrl + X "));
Cutact-> setstatustip (TR ("Cut the current selection's contents to"
"Clipboard "));
Connect (cutact, signal (triggered (), textedit, slot (cut ()));
Copyact = new qaction (qicon (":/images/copy.png"), TR ("& copy"), this );
Copyact-> setshortcut (TR ("Ctrl + C "));
Copyact-> setstatustip (TR ("copy the current selection's contents to
"
"Clipboard "));
Connect (copyact, signal (triggered (), textedit, slot (copy ()));
Pasteact = new qaction (qicon (":/images/paste.png"), TR ("& Paste"), this );
Pasteact-> setshortcut (TR ("Ctrl + V "));
Pasteact-> setstatustip (TR ("paste the clipboard's contents into
Current"
"Selection "));
Connect (pasteact, signal (triggered (), textedit, slot (paste ()));
Aboutact = new qaction (TR ("& about"), this );
Aboutact-> setstatustip (TR ("show the application's about box "));
Connect (aboutact, signal (triggered (), this, slot (about ()));
Aboutqtact = new qaction (TR ("about & QT"), this );
Aboutqtact-> setstatustip (TR ("show the QT library's about box "));
Connect (aboutqtact, signal (triggered (), qapp, slot (aboutqt ()));
4. If you create a Main Menu
A: use the help of the preceding qaction to create the main menu.
Quote:
Filemenu = menubar ()-> addmenu (TR ("& file "));
Filemenu-> addaction (newact );
Filemenu-> addaction (openact );
Filemenu-> addaction (saveact );
Filemenu-> addaction (saveasact );
Filemenu-> addseparator ();
Filemenu-> addaction (exitact );
Editmenu = menubar ()-> addmenu (TR ("& edit "));
Editmenu-> addaction (cutact );
Editmenu-> addaction (copyact );
Editmenu-> addaction (pasteact );
Menubar ()-> addseparator ();
Helpmenu = menubar ()-> addmenu (TR ("& Help "));
Helpmenu-> addaction (aboutact );
Helpmenu-> addaction (aboutqtact );
5. If you create a toolbar
A: use the help of the preceding qaction to create a toolbar.
Quote:
Filetoolbar = addtoolbar (TR ("file "));
Filetoolbar-> addaction (newact );
Filetoolbar-> addaction (openact );
Filetoolbar-> addaction (saveact );
Edittoolbar = addtoolbar (TR ("edit "));
Edittoolbar-> addaction (cutact );
Edittoolbar-> addaction (copyact );
Edittoolbar-> addaction (pasteact );
6. How to save the configuration using the configuration file
A: Use the qsettings class.
Quote:
Qsettings settings ("trolltech", "application example ");
Qpoint Pos = settings. Value ("POS", qpoint (200,200). topoint ();
Qsize size = settings. Value ("size", qsize (400,400). tosize ();
Quote:
Qsettings settings ("trolltech", "application example ");
Settings. setvalue ("POS", pos ());
Settings. setvalue ("size", size ());
7. How to Use the warning and information dialog box
A: Use the static method of the qmessagebox class.
Quote:
Int ret = qmessagebox: Warning (this, TR ("application "),
TR ("the document has been modified./N"
"Do you want to save your changes? "),
Qmessagebox: Yes | qmessagebox: default,
Qmessagebox: No,
Qmessagebox: Cancel | qmessagebox: escape );
If (ret = qmessagebox: Yes)
Return save ();
Else if (ret = qmessagebox: Cancel)
Return false;
8. How to make the general dialog box Culture
A: culture in the dialog box
For example, the text-related content of qcolordialog is mainly in the qcolordialog. cpp file.
, We can generate a TS file from qcolordialog. cpp with lupdate, and then use custom
TS file translation, and then use lrelease to generate a. QM file. Of course, the main program will
The change must support multiple languages. You can use this. QM file.
In addition, there is a faster method, and after the source code is unlocked, there is a directory translations, below
There are some. Ts and. QM files. We copy one:
Quote:
CP src/translations/qt_untranslated.ts./qt_zh_cn.ts
Then, we use linguist to open the qt_zh_cn.ts and translate it. After the translation is complete,
After saving, use the lrelease command to generate qt_zh_cn.qm. In this way, we add it to our
In the QT project, the system dialog box, menu, and other items are displayed in English by default.
It is displayed as Chinese.
9. Why is there no terminal output in Qt in windows?
A: Add the following configuration items to the. Pro file.
Quote:
Win32: config + = console
10. How does QT 4 for X11 opensource use static links?
A: add the-static option during compilation and installation.
Quote:
./Configure-static // you must add the static Option
Gmake
Gmake install
Then, add the static option to the MAKEFILE file or add qmake_lflags to the. Pro file.
+ =-Static, you can connect to the static library.
11. What should I do if I want to directly use Chinese characters in the source code instead of using the tr () function for conversion?
A: The following three statements are added to the main function, but they are not recommended.
Quote:
Qtextcodec: setcodecforlocale (qtextcodec: codecforname ("UTF-8 "));
Qtextcodec: setcodecforcstrings (qtextcodec: codecforname ("UTF-8 "));
Qtextcodec: setcodecfortr (qtextcodec: codecforname ("UTF-8 "));
Or
Quote:
Qtextcodec: setcodecforlocale (qtextcodec: codecforname ("GBK "));
Qtextcodec: setcodecforcstrings (qtextcodec: codecforname ("GBK "));
Qtextcodec: setcodecfortr (qtextcodec: codecforname ("GBK "));
Whether to use GBK or UTF-8 depends on the internal code used in the Chinese characters in the source file
In this way, you can directly use Chinese characters in the source file, for example:
Quote:
Qmessagebox: Information (null, "information", "demo information about this software ",
Qmessagebox: OK, qmessagebox: nobuttons );
12. Why can't I connect the database when I publish the database program I developed to another machine?
A: This is because the program cannot find the database plug-in. The solution is as follows:
Add the following statement to the main function:
Quote:
Qapplication: addlibrarypath (strpluginspath ");
Strpluginspath is the directory where the plug-in is located. For example, the directory is/myapplication/plugins.
Put the required SQL driver, such as qsqlmysql. dll, qsqlodbc. dll, or the corresponding. So file
To
/Myapplication/plugins/sqldrivers/
Directory.
This is a solution and a common solution, that is, to write data in the executable file directory.
The QT. conf file writes some directory configurations related to the system to the QT. conf file.
Test QT. conf in QT document reference.
13. How to create the DLL (. So) used by QT and how to use this DLL (. So)
A: When creating a DLL, the project uses the Lib template.
Quote:
Template = lib
The source file is the same as the common source file. Note that the header file and the source file are separated because
This header file is required when the program uses this DLL.
When using this DLL, introduce the DLL header file in this project source file and add it to the. Pro file.
The following configuration items:
Quote:
Libs + =-lyourdlllibpath-lyourdlllibname
In Windows and Linux (the DLL file generated in Windows is yourdlllibname. dll
In Linux, libyourdlllibname. So is generated. Note: For DLL program writing,
Follow the rules set by the compiler at each platform level.
14. How to start an external program
Answer: 1. Use the qprocess: startdetached () method to return immediately after the external program is started;
2. Use qprocess: Execute (), but when this method is used, the program will be blocked most until this method is executed
When the program of the row ends, the qprocess and qthread classes can be used in combination.
To prevent blocking caused by calling in the main thread.
First, inherit a class from qthread and re-implement the run () function:
Quote:
Class mythread: Public qthread
{
Public:
Void run ();
};
Void mythread: Run ()
{
Qprocess: Execute ("notepad.exe ");
}
In this way, a member variable of the mythread type can be defined during use and called
Start () method:
Quote:
Class ...............
{...........
Mythread thread;
............
};
.....................
Thread. Start ();
15. How to print reports
A: QT currently supports few libraries for report printing. However, a work und is to use
XML + XSLT + XSL-FO for report design, XML output data, XML data is converted
XSL-FO format reports, because the current browser does not directly support XSL-FO format display, so temporarily
When available tools (APACHE fop, Java) to convert the XSL-FO to PDF document for printing
Changes and prints are done by fop, generating reports in XSL-FO format can be generated by QT, or by other
For example, a tool (html2fo) is used to convert HTML to XSL-fo.
16. How to display icons in the system tray Area
A: Use the qsystemtrayicon class in version 4.2 and later.
17. How to output logs to files
A: (provided by Myer)
Quote:
Void mymessageoutput (qtmsgtype type, const char * MSG)
{
Switch (type ){
Case qtdebugmsg:
// Write the file;
Break;
Case qtwarningmsg:
Break;
Case qtfatalmsg:
Abort ();
}
}
Int main (INT argc, char ** argv)
{
Qapplication app (argc, argv );
Qinstallmsghandler (mymessageoutput );
......
Return app.exe C ();
}
Qdebug (), qwarning (), and qfatal () correspond to the above three types respectively.
18. How to compile images into executable programs
A: Use the. qrc file.
Write the. qrc file, for example:
Res. qrc
Quote:
<! Doctype RCC> <RCC version = "1.0">
<Qresource>
<File> images/copy.png </File>
<File> images/cut.png </File>
<File> images/new.png </File>
<File> images/open.png </File>
<File> images/paste.png </File>
<File> images/save.png </File>
</Qresource>
</RCC>
Then add the following code to. Pro:
Quote:
Resources = res. qrc
Use in the program:
Quote:
...
: Images/copy.png
...
19. How to create an irregular form or part
A: refer to the following post.
Http://www.qtcn.org/bbs/read.php? Tid = 8681
20. "qsqldatabaseprivate: removedatabase: Connection" appears when you delete a database.
'Xxxx' is still in use, all queries will cease to work"
A: This error occurs because the variable scope with the connection name XXXX has not ended.
The method is to use it after the scopes of all the database component variables connected with XXXX are completed.
Qsqldatabase: removedatabae ("XXXX") to delete the connection.
21. How to display an image and make it zoom along with the form
A: The following is a class imagewidget derived from qwidget to set its background to an image,
And can change with the form change, in fact, from the following code can be derived from many other methods, if
If necessary, other classes can be generated from this class.
Header file: imagewidget. HPP
Quote:
# Ifndef imagewidget_hpp
# Define imagewidget_hpp
# Include <qtcore>
# Include <qtgui>
Class imagewidget: Public qwidget
{
Q_object
Public:
Imagewidget (qwidget * parent = 0, QT: windowflags f = 0 );
Virtual ~ Imagewidget ();
Protected:
Void resizeevent (qresizeevent * event );
PRIVATE:
Qimage _ image;
};
# Endif
CPP file: imagewidget. cpp
Quote:
# Include "imagewidget. HPP"
Imagewidget: imagewidget (qwidget * parent, QT: windowflags F)
: Qwidget (parent, F)
{
_ Image. Load ("image/image_background ");
Setautofillbackground (true); // This attribute must be set
Qpalette pal (palette ());
Pal. setbrush (qpalette: window,
Qbrush (_ image. Scaled (SIZE (), QT: ignoreaspectratio,
Qt: smoothtransformation )));
Setpalette (PAL );
}
Imagewidget ::~ Imagewidget ()
{
}
// Set the background as the form changes
Void imagewidget: resizeevent (qresizeevent * event)
{
Qwidget: resizeevent (event );
Qpalette pal (palette ());
Pal. setbrush (qpalette: window,
Qbrush (_ image. Scaled (Event-> size (), QT: ignoreaspectratio,
Qt: smoothtransformation )));
Setpalette (PAL );
}
22. How to read serial port information in Windows
A: You can use the registry to read qt4.1.0 and obtain the serial port 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.