常見的幾個Qt程式設計問題的處理
阿新 • • 發佈:2018-11-09
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(":/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("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(":/images/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(":/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、如果建立主選單
答:採用上面的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
10、Qt 4 for X11 OpenSource版如何靜態連結?
答:編譯安裝的時候加上-static選項
QUOTE: ./configure -static //一定要加static選項
gmake
gmake install
然後,在Makefile檔案中加 static 選項,就可以連線靜態庫了。
答:重新實現這個窗體的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(":/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("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(":/images/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(":/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、如果建立主選單
答:採用上面的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
10、Qt 4 for X11 OpenSource版如何靜態連結?
答:編譯安裝的時候加上-static選項
QUOTE: ./configure -static //一定要加static選項
gmake
gmake install
然後,在Makefile檔案中加 static 選項,就可以連線靜態庫了。