Make your Qt desktop program look more native (6): cross-platform technology

Source: Internet
Author: User

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.

 
 
  1. MainWindow::MainWindow(QWidget *parent) : 
  2.     QMainWindow(parent), 
  3.     ui(new Ui::MainWindow) 
  4.     ui->setupUi(this); 
  5.     this->setWindowTitle("untitled [*]"); 
  6.  
  7.     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:

 
 
  1. 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

Related Article

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.