1、若是在窗體關閉前自行判斷是否可關閉
答:從新實現這個窗體的closeEvent()函數,加入判斷操做
Quote:
void MainWindow::closeEvent(QCloseEvent *event)
{
if (maybeSave())
{
writeSettings();
event->accept();
}
else
{
event->ignore();
}
}
2、如何用打開和保存文件對話
答:使用QFileDialog
Quote:
QString fileName = QFileDialog::getOpenFileName(this);
if (!fileName.isEmpty())
{
loadFile(fileName);
}
Quote:
QString fileName = QFileDialog::getSaveFileName(this);
if (fileName.isEmpty())
{
return false;
}
3、若是建立Actions(可在菜單和工具欄裏使用這些Action)
答:
Quote:
newAct = new QAction(QIcon(":/p_w_picpaths/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(":/p_w_picpaths/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(":/p_w_picpaths/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(":/p_w_picpaths/cut.png"), tr("Cu&t"), 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(":/p_w_picpaths/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(":/p_w_picpaths/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、若是建立主菜單
答:採用上面的QAction的幫助,建立主菜單
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、若是建立工具欄
答:採用上面的QAction的幫助,建立工具欄
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、如何使用警告、信息等對話框
答:使用QMessageBox類的靜態方法
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、如何使通用對話框中文化
答:對話框的中文化
好比說,QColorDialog的與文字相關的部分,主要在qcolordialog.cpp文件中,咱們能夠從qcolordialog.cpp用 lupdate生成一個ts文件,而後用自定義這個ts文件的翻譯,再用lrelease生成一個.qm文件,固然了,主程序就要改變要支持多國語言了,使用這個.qm文件就能夠了。
另外,還有一個更快的方法,在源代碼解開後有一個目錄translations,下面有一些.ts, .qm文件,咱們拷貝一個:
Quote:
cp src/translations/qt_untranslated.ts ./qt_zh_CN.ts
而後,咱們就用Linguist打開這個qt_zh_CN.ts,進行翻譯了,翻譯完成後,保存後,再用lrelease命令生成qt_zh_CN.qm,這樣,咱們把它加入到咱們的qt project中,那些系統的對話框,菜單等等其它的默認是英文的東西就能顯示成中文了。
9、在Windows下Qt裏爲何沒有終端輸出?
答:把下面的配置項加入到.pro文件中
Quote:
win32:CONFIG += console
十、Qt 4 for X11 OpenSource版如何靜態連接?
答:編譯安裝的時候加上-static選項
Quote:
./configure -static //必定要加static選項
gmake
gmake install
而後,在Makefile文件中加 static 選項或者在.pro文件中加上QMAKE_LFLAGS += -static,就能夠鏈接靜態庫了。
十一、想在源代碼中直接使用中文,而不使用tr()函數進行轉換,怎麼辦?
答:在main函數中加入下面三條語句,但並不提倡
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,依源文件中漢字使用的內碼而定
這樣,就可在源文件中直接使用中文,好比:
Quote:
QMessageBox::information(NULL, "信息", "關於本軟件的演示信息", QMessageBox::Ok, QMessageBox::NoButtons);
十二、爲何將開發的使用數據庫的程序發佈到其它機器就鏈接不上數據庫?
答:這是因爲程序找不到數據庫插件而致,可照以下解決方法:
在main函數中加入下面語句:
Quote:
QApplication::addLibraryPath(strPluginsPath");
strPluginsPath是插件所在目錄,好比此目錄爲/myapplication/plugins
則將須要的sql驅動,好比qsqlmysql.dll, qsqlodbc.dll或對應的.so文件放到
/myapplication/plugins/sqldrivers/
目錄下面就好了
這是一種解決方法,還有一種通用的解決方法,即在可執行文件目錄下寫qt.conf文件,把系統相關的一些目錄配置寫到qt.conf文件裏,詳細狀況情參考Qt Document Reference裏的qt.conf部分
1三、如何建立QT使用的DLL(.so)以及如何使用此DLL(.so)
答:建立DLL時其工程使用lib模板
Quote:
TEMPLATE=lib
而源文件則和使用普通的源文件同樣,注意把頭文件和源文件分開,由於在其它程序使用此DLL時須要此頭文件
在使用此DLL時,則在此工程源文件中引入DLL頭文件,並在.pro文件中加入下面配置項:
Quote:
LIBS += -Lyourdlllibpath -lyourdlllibname
Windows下和Linux下一樣(Windows下生成的DLL文件名爲yourdlllibname.dll而在Linux下生成的爲libyourdlllibname.so。注意,關於DLL程序的寫法,聽從各平臺級編譯器所定的規則。
1四、如何啓動一個外部程序
答:一、使用QProcess::startDetached()方法,啓動外部程序後當即返回;
二、使用QProcess::execute(),不過使用此方法時程序會最阻塞直到此方法執行的程序結束後返回,這時候可以使用QProcess和QThread這兩個類結合使用的方法來處理,以防止在主線程中調用而致使阻塞的狀況
先從QThread繼承一個類,從新實現run()函數:
Quote:
class MyThread : public QThread
{
public:
void run();
};
void MyThread::run()
{
QProcess::execute("notepad.exe");
}
這樣,在使用的時候則可定義一個MyThread類型的成員變量,使用時調用其start()方法:
Quote:
class ...............
{...........
MyThread thread;
............
};
.....................
thread.start();
1五、如何打印報表
答:Qt 目前對報表打印支持的庫還不多,不過有種變通的方法,就是使用XML+XSLT+XSL-FO來進行報表設計,XML輸出數據,用XSLT將XML數據轉換爲XSL-FO格式的報表,因爲如今的瀏覽器不直接支持XSL-FO格式的顯示,因此暫時可用工具(Apache FOP, Java作的)將XSL-FO轉換爲PDF文檔來進行打印,轉換和打印由FOP來作,生成XSL-FO格式的報表能夠由Qt來生成,也能夠由其它內容轉換過來,好比有工具(html2fo)將HTML轉換爲XSL-FO。
1六、如何在系統托盤區顯示圖標
答:在4.2及其以上版本中使用QSystemTrayIcon類來實現
1七、怎樣將日誌輸出到文件中
答:(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()分別對應以上三種type。
1八、如何將圖像編譯到可執行程序中去
答:使用.qrc文件
寫.qrc文件,例如:
res.qrc
Quote:
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file>p_w_picpaths/copy.png</file>
<file>p_w_picpaths/cut.png</file>
<file>p_w_picpaths/new.png</file>
<file>p_w_picpaths/open.png</file>
<file>p_w_picpaths/paste.png</file>
<file>p_w_picpaths/save.png</file>
</qresource>
</RCC>
而後在.pro中加入下面代碼:
Quote:
RESOURCES = res.qrc
在程序中使用:
Quote:
...
:p_w_picpaths/copy.png
...
20、刪除數據庫時出現"QSqlDatabasePrivate::removeDatabase: connection 'xxxx' is still in use, all queries will cease to work"該如何處理
答:出現此種錯誤是由於使用了鏈接名字爲xxxx的變量做用域沒有結束,解決方法是在全部使用了xxxx鏈接的數據庫組件變量的做用域都結束後再使用QSqlDatabase::removeDatabae("xxxx")來刪除鏈接。
2一、如何顯示一個圖片並使其隨窗體同步縮放
答:下面給出一個從QWidget派生的類ImageWidget,來設置其背景爲一個圖片,並可隨着窗體改變而改變,其實從下面的代碼中能夠引伸出其它許多方法,若是須要的話,能夠從這個類再派生出其它類來使用。
頭文件: 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 _p_w_picpath;
};
#endif
CPP文件: ImageWidget.cpp
Quote:
#include "ImageWidget.hpp"
ImageWidget::ImageWidget(QWidget *parent, Qt::WindowFlags f)
: QWidget(parent, f)
{
_p_w_picpath.load("p_w_picpath/p_w_picpath_background");
setAutoFillBackground(true); // 這個屬性必定要設置
QPalette pal(palette());
pal.setBrush(QPalette::Window,
QBrush(_p_w_picpath.scaled(size(), Qt::IgnoreAspectRatio,
Qt::SmoothTransformation)));
setPalette(pal);
}
ImageWidget::~ImageWidget()
{
}
// 隨着窗體變化而設置背景
void ImageWidget::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
QPalette pal(palette());
pal.setBrush(QPalette::Window,
QBrush(_p_w_picpath.scaled(event->size(), Qt::IgnoreAspectRatio,
Qt::SmoothTransformation)));
setPalette(pal);
}
2二、Windows下如何讀串口信息
答:可經過註冊表來讀qt4.1.0 讀取註冊表獲得 串口信息的方法!
23.
參考網址:
http://www.ibm.com/developerworks/cn/linux/guitoolkit/qt/i18n/ QT程序國際化的過程 1.在全部須要採用雙語或多語的地方採用QObject::tr("")將要顯示的字符串包起來。 這裏的字符串最好爲英文,ASCII的編碼在哪裏都不會解碼錯。 tr()的做用之一是從.qm文件中取出與其所包含的內容對應的信息。 有些地方不宜採用tr(),好比定義某些變量使用的字符串,如: static const char* strings[] = {T_TR_NOOP("Hello"),QT_TR_NOOP("World")}; 能夠採用QT_TR_NOOP() 和 QT_TRANSLATE_NOOP() 來標誌它們。 前者用於單個字 符串,後者用於多個字符串。 若是要使用printf/sprintf之類的函數動態生成字符串,須要替換爲arg(),如: QString s = tr("Button %1").arg(i);