Now we will provide some cross-platform technologies related to Qt. Using these technologies can make your applications look more in line with the platform's habits. This article is not a complete example, but a lot of small entries. I hope you can understand these simple tips and easily apply them to your own programs. Many GUI problems are due to a lack of knowledge. You know, it can be easily implemented. If you don't know, it will become very clumsy. Today's goal is not to let examples, but to let you "know ".
1. display content modification
Sometimes we need to make a text editor. If the content of the text editor is modified, a prompt will be displayed in the title bar, such as adding. This function is provided by Qt. For example:
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1I304H95-0.png "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1I3045562-1.png "/>
When we enter text in the text box, a * is displayed in the window, indicating that the content has been modified and saved by the user. In fact, this implementation is very simple. The setWindowTitle () function of Qt has a placeholder that can reserve the * position.
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- this->setWindowTitle("untitled [*]");
-
- connect(ui->textEdit->document(), SIGNAL(modificationChanged(bool)), SLOT(setWindowModified(bool)));
- }
Similar to the above Code. The window title is set to untitled [*], and [*] is where * appears in the future. Finally, we connect the content change signal of QTextEdit with the setWindowModified () slot. As long as setWindowModified () is set to true, the content adapted * is automatically displayed for us.
2. Ask the program to remind users
We usually have this requirement: Our program can run only one instance. If you try to run the same program again after the user has run it, the program will have a reminder function. In Windows, the window icon of the taskbar flashes, while in Mac, the icon on Dash jumps:
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1I3043W3-2.png "/>
This function is platform-related. If we want to write such a program, we have to call the system API. Fortunately, Qt also provides us with this function, that is, the QApplication: alert () function. This function is a static function of the QApplication class. Therefore, it can be used anywhere. This function is used to give your program a reminder.
Iii. System Tray
Modern Operating systems generally have system pallets. Windows, KDE, and so on. Although the Mac system does not, there is a similar function, that is, a prompt can appear in the upper right corner of the program.
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1I3043437-3.png "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1I30425c-4.png "/>
To implement this function, Qt provides the QSystemTrayIcon class for us. For detailed usage, refer to the document. Note that the Mac icon must be black and white.
4. Obtain the storage location
If we want to use our own program to save files, we usually have a default storage location. In general, Windows programs are usually in my documents by default. How can we obtain this storage path? Qt has a convenient qinitopservices class. There is a static function in this class:
- QString QDesktopServices::storageLocation(StandardLocation type)
With this function, we can obtain the default paths such as desktop, music, and cache. In this way, our program can be integrated with the system.
5. Call the system default program to open the link
Sometimes we want to use the system default program to open the link. For example, you can call the default browser to open a webpage and call the default mail client to send emails. To implement this function, we need to use the q1_topservices: openUrl () function. If you want to open a link starting with mailto, the system's default mail processing program will be automatically called.
This article is from the "bean space" blog, please be sure to keep this source http://devbean.blog.51cto.com/448512/516696