新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > QT 技術(shù)應(yīng)用常用問題解答

        QT 技術(shù)應(yīng)用常用問題解答

        作者: 時(shí)間:2016-09-12 來源:網(wǎng)絡(luò) 收藏

        1、如果在窗體關(guān)閉前自行判斷是否可關(guān)閉

        本文引用地址:http://www.104case.com/article/201609/303382.htm

        答:重新實(shí)現(xiàn)這個(gè)窗體的closeEvent()函數(shù),加入判斷操作

        Quote:

        void MainWindow::closeEvent(QCloseEvent *event)

        {

        if (maybeSave())

        {

        writeSettings();

        event->accept();

        }

        else

        {

        event->ignore();

        }

        }

        2、如何用打開和保存文件對(duì)話

        答:使用QFileDialog

        Quote:

        QString fileName = QFileDialog::getOpenFileName(this);

        if (!fileName.isEmpty())

        {

        loadFile(fileName);

        }

        Quote:

        QString fileName = QFileDialog::getSaveFileName(this);

        if (fileName.isEmpty())

        {

        return false;

        }

        3、如果創(chuàng)建Actions(可在菜單和工具欄里使用這些Action)

        答:

        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(Exit), 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(Cut), this);

        cutAct->setShortcut(tr(Ctrl+X));

        cutAct->setStatusTip(tr(Cut the current selection's contents to the

        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 the

        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 the 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、如果創(chuàng)建主菜單

        答:采用上面的QAction的幫助,創(chuàng)建主菜單

        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、如果創(chuàng)建工具欄

        答:采用上面的QAction的幫助,創(chuàng)建工具欄

        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、如何使用配置文件保存配置

        答:使用QSettings類

        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、如何使用警告、信息等對(duì)話框

        答:使用QMessageBox類的靜態(tài)方法

        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、如何使通用對(duì)話框中文化

        答:對(duì)話框的中文化

        比 如說,QColorDialog的與文字相關(guān)的部分,主要在qcolordialog.cpp文件中,我們可以從qcolordialog.cpp用 lupdate生成一個(gè)ts文件,然后用自定義這個(gè)ts文件的翻譯,再用lrelease生成一個(gè).qm文件,當(dāng)然了,主程序就要改變要支持多國語言了, 使用這個(gè).qm文件就可以了。

        另外,還有一個(gè)更快的方法,在源代碼解開后有一個(gè)目錄translations,下面有一些.ts, .qm文件,我們拷貝一個(gè):

        Quote:

        cp src/translations/qt_untranslated.ts ./qt_zh_CN.ts

        然 后,我們就用Linguist打開這個(gè)qt_zh_CN.ts,進(jìn)行翻譯了,翻譯完成后,保存后,再用lrelease命令生成qt_zh_CN.qm, 這樣,我們把它加入到我們的qt project中,那些系統(tǒng)的對(duì)話框,菜單等等其它的默認(rèn)是英文的東西就能顯示成中文了。

        9、在Windows下Qt里為什么沒有終端輸出?

        答:把下面的配置項(xiàng)加入到.pro文件中

        Quote:

        win32:CONFIG += console

        10、Qt 4 for X11 OpenSource版如何靜態(tài)鏈接?

        答:編譯安裝的時(shí)候加上-static選項(xiàng)

        Quote:

        ./configure -static //一定要加static選項(xiàng)

        gmake

        gmake install

        然后,在Makefile文件中加 static 選項(xiàng)或者在.pro文件中加上QMAKE_LFLAGS += -static,就可以連接靜態(tài)庫了。

        11、想在源代碼中直接使用中文,而不使用tr()函數(shù)進(jìn)行轉(zhuǎn)換,怎么辦?

        答:在main函數(shù)中加入下面三條語句,但并不提倡

        Quote:

        QTextCodec::setCodecForLocale(QTextCodec::codecForName(UTF-8));

        QTextCodec::setCodecForCStrings(QTextCodec::codecForName(UTF-8));

        QTextCodec::setCodecForTr(QTextCodec::codecForName(UTF-8));

        或者

        Quote:

        QTextCodec::setCodecForLocale(QTextCodec::codecForName(GBK));

        QTextCodec::setCodecForCStrings(QTextCodec::codecForName(GBK));

        QTextCodec::setCodecForTr(QTextCodec::codecForName(GBK));

        使用GBK還是使用UTF-8,依源文件中漢字使用的內(nèi)碼而定

        這樣,就可在源文件中直接使用中文,比如:

        Quote:

        QMessageBox::information(NULL, 信息, 關(guān)于本軟件的演示信息, QMessageBox::Ok, QMessageBox::NoButtons);

        12、為什么將開發(fā)的使用數(shù)據(jù)庫的程序發(fā)布到其它機(jī)器就連接不上數(shù)據(jù)庫?

        答:這是由于程序找不到數(shù)據(jù)庫插件而致,可照如下解決方法:

        在main函數(shù)中加入下面語句:

        Quote:

        QApplication::addLibraryPath(strPluginsPath);

        strPluginsPath是插件所在目錄,比如此目錄為/myapplication/plugins

        則將需要的sql驅(qū)動(dòng),比如qsqlmysql.dll, qsqlodbc.dll或?qū)?yīng)的.so文件放到

        /myapplication/plugins/sqldrivers/

        目錄下面就行了

        這是一種解決方法,還有一種通用的解決方法,即在可執(zhí)行文件目錄下寫qt.conf文件,把系統(tǒng)相關(guān)的一些目錄配置寫到qt.conf文件里,詳細(xì)情況情參考Qt Document Reference里的qt.conf部分

        13、如何創(chuàng)建QT使用的DLL(.so)以及如何使用此DLL(.so)

        答:創(chuàng)建DLL時(shí)其工程使用lib模板

        Quote:

        TEMPLATE=lib

        而源文件則和使用普通的源文件一樣,注意把頭文件和源文件分開,因?yàn)樵谄渌绦蚴褂么薉LL時(shí)需要此頭文件

        在使用此DLL時(shí),則在此工程源文件中引入DLL頭文件,并在.pro文件中加入下面配置項(xiàng):

        Quote:

        LIBS += -Lyourdlllibpath -lyourdlllibname

        Windows下和Linux下同樣(Windows下生成的DLL文件名為yourdlllibname.dll而在Linux下生成的為libyourdlllibname.so。注意,關(guān)于DLL程序的寫法,遵從各平臺(tái)級(jí)編譯器所定的規(guī)則。

        14、如何啟動(dòng)一個(gè)外部程序

        答:1、使用QProcess::startDetached()方法,啟動(dòng)外部程序后立即返回;

        2、使用QProcess::execute(),不過使用此方法時(shí)程序會(huì)最阻塞直到此方法執(zhí)行的程序結(jié)束后返回,這時(shí)候可使用QProcess和QThread這兩個(gè)類結(jié)合使用的方法來處理,以防止在主線程中調(diào)用而導(dǎo)致阻塞的情況

        先從QThread繼承一個(gè)類,重新實(shí)現(xiàn)run()函數(shù):

        Quote:

        class MyThread : public QThread

        {

        public:

        void run();

        };

        void MyThread::run()

        {

        QProcess::execute(notepad.exe);

        }

        這樣,在使用的時(shí)候則可定義一個(gè)MyThread類型的成員變量,使用時(shí)調(diào)用其start()方法:

        Quote:

        class ...............

        {...........

        MyThread thread;

        ............

        };

        .....................

        thread.start();

        15、如何打印報(bào)表

        答:Qt 目前對(duì)報(bào)表打印支持的庫還很少,不過有種變通的方法,就是使用XML+XSLT+XSL-FO來進(jìn)行報(bào)表設(shè)計(jì),XML輸出數(shù)據(jù),用XSLT將XML數(shù)據(jù)轉(zhuǎn) 換為XSL-FO格式的報(bào)表,由于現(xiàn)在的瀏覽器不直接支持XSL-FO格式的顯示,所以暫時(shí)可用工具(Apache FOP, Java做的)將XSL-FO轉(zhuǎn)換為PDF文檔來進(jìn)行打印,轉(zhuǎn)換和打印由FOP來做,生成XSL-FO格式的報(bào)表可以由Qt來生成,也可以由其它內(nèi)容轉(zhuǎn)換 過來,比如有工具(html2fo)將HTML轉(zhuǎn)換為XSL-FO。

        16、如何在系統(tǒng)托盤區(qū)顯示圖標(biāo)

        答:在4.2及其以上版本中使用QSystemTrayIcon類來實(shí)現(xiàn)

        17、怎樣將日志輸出到文件中

        答:(myer提供)

        Quote:

        void myMessageOutput( QtMsgType type, const char *msg )

        {

        switch ( type ) {

        case QtDebugMsg:

        //寫入文件;

        break;

        case QtWarningMsg:

        break;

        case QtFatalMsg:

        abort();

        }

        }

        int main( int argc, char** argv )

        {

        QApplication app( argc, argv );

        qInstallMsgHandler( myMessageOutput );

        ......

        return app.exec();

        }

        qDebug(), qWarning(), qFatal()分別對(duì)應(yīng)以上三種type。

        18、如何將圖像編譯到可執(zhí)行程序中去

        答:使用.qrc文件

        寫.qrc文件,例如:

        res.qrc

        Quote:

        images/copy.png

        images/cut.png

        images/new.png

        images/open.png

        images/paste.png

        images/save.png

        然后在.pro中加入下面代碼:

        Quote:

        RESOURCES = res.qrc

        在程序中使用:

        Quote:

        ...

        :images/copy.png

        ...

        19、如何制作不規(guī)則形狀的窗體或部件

        答:請(qǐng)參考下面的帖子

        http://www.qtcn.org/bbs/read.php?tid=8681

        20、刪除數(shù)據(jù)庫時(shí)出現(xiàn)QSqlDatabasePrivate::removeDatabase: connection 'xxxx' is still in use, all queries will cease to work該如何處理

        答:出現(xiàn)此種錯(cuò)誤是因?yàn)槭褂昧诉B接名字為xxxx的變量作用域沒有結(jié)束,解決方法是在所有使用了xxxx連接的數(shù)據(jù)庫組件變量的作用域都結(jié)束后再使用QSqlDatabase::removeDatabae(xxxx)來刪除連接。

        21、如何顯示一個(gè)圖片并使其隨窗體同步縮放

        答:下面給出一個(gè)從QWidget派生的類ImageWidget,來設(shè)置其背景為一個(gè)圖片,并可隨著窗體改變而改變,其實(shí)從下面的代碼中可以引申出其它許多方法,如果需要的話,可以從這個(gè)類再派生出其它類來使用。

        頭文件: ImageWidget.hpp

        Quote:

        #ifndef IMAGEWIDGET_HPP

        #define IMAGEWIDGET_HPP

        #include

        #include

        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文件: ImageWidget.cpp

        Quote:

        #include ImageWidget.hpp

        ImageWidget::ImageWidget(QWidget *parent, Qt::WindowFlags f)

        : QWidget(parent, f)

        {

        _image.load(image/image_background);

        setAutoFillBackground(true); // 這個(gè)屬性一定要設(shè)置

        QPalette pal(palette());

        pal.setBrush(QPalette::Window,

        QBrush(_image.scaled(size(), Qt::IgnoreAspectRatio,

        Qt::SmoothTransformation)));

        setPalette(pal);

        }

        ImageWidget::~ImageWidget()

        {

        }

        // 隨著窗體變化而設(shè)置背景

        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、Windows下如何讀串口信息

        答:可通過注冊(cè)表來讀qt4.1.0 讀取注冊(cè)表得到 串口信息的方法!



        關(guān)鍵詞:

        評(píng)論


        相關(guān)推薦

        技術(shù)專區(qū)

        關(guān)閉
        主站蜘蛛池模板: 辽宁省| 镇平县| 临高县| 岑巩县| 师宗县| 池州市| 左贡县| 金山区| 桃江县| 镶黄旗| 焦作市| 大竹县| 吉隆县| 宜兰市| 湾仔区| 射洪县| 康定县| 贵南县| 蒲城县| 鄂温| 珠海市| 郸城县| 芜湖市| 乌兰察布市| 龙海市| 乌鲁木齐市| 防城港市| 盐城市| 翁源县| 包头市| 鹤庆县| 万荣县| 江口县| 中超| 苍梧县| 融水| 浑源县| 临猗县| 襄汾县| 稷山县| 马边|