LinuxÏÂʹÓÃQt¿ª·¢Í¼ÐνçÃæÓ¦ÓõĻù±¾ÉèÖÃÖ¸ÄÏ
linuxÏÂʹÓÃqt¿ª·¢Í¼ÐνçÃæÓ¦ÓõĻù±¾ÉèÖÃÖ¸ÄÏ
Ò»¡¢Ð¡Ðò
QtÊÇÒ»Ì׿çƽ̨µÄC++¿â£¬ËüÌṩÁ˸»ºñµÄͼÐνçÃ濪·¢×é¼þ£¬ÒÔ¼°Ðí¶àÆäËûµÄ¹¦Ð§Ä£¿é£¬ÊÊÓÃÓÚ¿ª·¢¿çƽ̨µÄͼÐνçÃæÓ¦ÓóÌÐò¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚLinuxϵͳϾÙÐÐQt¿ª·¢£¬²¢¸ø³öһЩ»ù±¾µÄÉèÖÃÖ¸ÄϺʹúÂëʾÀý¡£
¶þ¡¢×°ÖÃQt¿ª·¢ÇéÐÎ
ÏÂÔØQt
ÔÚQt¹Ù·½ÍøÕ¾ÉÏ (http://www.qt.io/) ÏÂÔغÏÊʵÄQt¿ª·¢Õ߰档ѡÔñºÏÊʵİ汾ºÍ×°Öðü£¬²¢Æ¾Ö¤¹Ù·½ÎĵµµÄÖ¸Òý¾ÙÐÐ×°Öá£×°ÖÃÀú³ÌÖпÉÒÔÑ¡Ôñ×°ÖÃËùÐèµÄ×é¼þºÍʾÀý´úÂë¡£
×°ÖñàÒëÆ÷ºÍ¹¹½¨¹¤¾ß
Qt¹Ù·½ÍƼöʹÓÃGCC×÷ΪC++±àÒëÆ÷¡£È·±£×°ÖÃÁ˺ÏÊÊ°æ±¾µÄGCC£¬ÔËÐÐÒÔÏÂÏÂÁî¾ÙÐмì²é£º
$ gcc --version
µÇ¼ºó¸´ÖÆ
ÁíÍ⣬ΪÁ˱àÒëºÍ¹¹½¨QtÓ¦Ó㬻¹ÐèҪװÖÃCMakeºÍMake¹¤¾ß¡£ÔËÐÐÒÔÏÂÏÂÁî¾ÙÐÐ×°Öãº
$ sudo apt-get install cmake make
µÇ¼ºó¸´ÖÆ
ÉèÖÃQt Creator
Qt¿ª·¢³£Óõļ¯³É¿ª·¢ÇéÐÎÊÇQt Creator¡£×°ÖÃÍêQtºó£¬¿ÉÒÔÖ±½ÓÔÚÓ¦ÓóÌÐò²Ëµ¥ÖÐÕÒµ½Qt Creator£¬Æô¶¯Ëü¼´¿É¡£
ÔÚQt CreatorÖУ¬µã»÷ “¹¤¾ß” -> “Ñ¡Ïî” -> “¹¹½¨ºÍÔËÐД -> “¹¤¾ßÁ´”£¬È·±£Ñ¡ÖÐÁ˺ÏÊʵıàÒëÆ÷¡£ÔÚ “¹¹½¨Ì×¼þ” Ñ¡ÏÖУ¬Ñ¡ÔñºÏÊʵÄQt°æ±¾¡£
Èý¡¢½¨ÉèÒ»¸ö¼òÆÓµÄQtÓ¦ÓÃ
н¨QtÏîÄ¿
ÔÚQt CreatorÖУ¬µã»÷ “Îļþ” -> “н¨Îļþ»òÏîÄ¿” -> “Ó¦ÓÔ£¬Ñ¡Ôñ “Qt WidgetsÓ¦ÓÔ¡£ÌîдÏîÄ¿Ãû³ÆºÍ·¾¶£¬µã»÷ “ÏÂÒ»²½”¡£
±àд´úÂë
ÔÚн¨µÄÏîÄ¿ÖУ¬·¿ª “mainwindow.h” Îļþ£¬±àдÈçÏ´úÂ룺
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); }; #endif // MAINWINDOW_H
µÇ¼ºó¸´ÖÆ
È»ºó£¬·¿ª “mainwindow.cpp” Îļþ£¬±àдÈçÏ´úÂ룺
#include "mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { } MainWindow::~MainWindow() { }
µÇ¼ºó¸´ÖÆ
±àÒëºÍÔËÐÐÓ¦ÓÃ
µã»÷ “¹¹½¨” -> “¹¹½¨ÏîÄ¿”£¬ÆÚ´ý±àÒëÍê³É¡£È»ºóµã»÷ “ÔËÐД -> “ÔËÐД£¬¼´¿ÉÆô¶¯Ó¦ÓóÌÐò¡£
ËÄ¡¢Ìí¼Ó½çÃæ×é¼þ
ÔÚQtÓ¦ÓÃÖУ¬¿ÉÒÔͨ¹ý½çÃæÉè¼ÆÆ÷À´Ìí¼ÓÖÖÖÖ×é¼þ¡£µã»÷ “ÏîÄ¿” -> “Ìí¼ÓД -> “Qt”£¬Ñ¡ÔñÐèÒªÌí¼ÓµÄ×é¼þ¡£
ÀýÈ磬ҪÌí¼ÓÒ»¸ö°´Å¥£¬¿ÉÒÔÑ¡Ôñ “Qt Widgets” -> “PushButton”£¬È»ºóÍÏקµ½´°¿ÚÖм´¿É¡£
Îå¡¢Ìí¼ÓÊÂÎñ´¦Àí
ÔÚmainwindow.hÎļþÖУ¬Ìí¼ÓÒ»¸ö²Ûº¯Êý£º
private slots: void onButtonClicked();
µÇ¼ºó¸´ÖÆ
ÔÚmainwindow.cppÎļþÖУ¬ÊµÏָòۺ¯Êý£º
void MainWindow::onButtonClicked() { // ´¦Àí°´Å¥µã»÷ÊÂÎñ }
µÇ¼ºó¸´ÖÆ
ÔÚmainwindow.cppµÄ½á¹¹º¯ÊýÖУ¬ÅþÁ¬°´Å¥µÄµã»÷ÐźÅÓë²Ûº¯Êý£º
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { QPushButton *button = new QPushButton("Click Me", this); connect(button, SIGNAL(clicked()), this, SLOT(onButtonClicked())); }
µÇ¼ºó¸´ÖÆ
Áù¡¢×ܽá
±¾ÎÄÏÈÈÝÁËÔÚLinuxÏÂÉèÖÃQt¿ª·¢ÇéÐεĻù±¾°ì·¨£¬²¢¸ø³öÁËÒ»¸ö½¨Éè¼òÆÓQtÓ¦ÓõÄʾÀý¡£Í¨¹ýѧϰºÍʵ¼ù£¬½øÒ»²½ÕÆÎÕQtµÄ¿ª·¢¼¼ÇɺÍÓ¦Óá£
¸½Â¼£º
ÍêÕûʾÀý´úÂëÇë²Î¿¼ÒÔÏÂÁ´½Ó£º[GitHub¿ÍÕ»](https://github.com/example/qt-basic-configuration)
£¨×¢£º¸ÃÎÄÕÂΪÐéÄâÖúÊÖ×Ô¶¯ÌìÉú£¬½ö¹©²Î¿¼¡££©
ÒÔÉϾÍÊÇLinuxÏÂʹÓÃQt¿ª·¢Í¼ÐνçÃæÓ¦ÓõĻù±¾ÉèÖÃÖ¸ÄϵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡