QT 技术应用常用问题解答
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(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、如果创建主菜单
答:采用上面的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 setting
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)
