资源描述:
《常见的几个Qt编程问题的处理》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、1、如果在窗体关闭前自行判断是否可关闭答:重新实现这个窗体的closeEvent()函数,加入判断操作引用voidMainWindow::closeEvent(QCloseEvent*event){ if(maybeSave()) { writeSettings(); event->accept(); } else { event->ignore(); }}2、如何用打开和保存文件对话答:使用QFileDialog引用 QStringfileName=QFileDialog
2、::getOpenFileName(this); if(!fileName.isEmpty()) { loadFile(fileName); }引用 QStringfileName=QFileDialog::getSaveFileName(this); if(fileName.isEmpty()) { returnfalse; }3、如果创建Actions(可在菜单和工具栏里使用这些Action)答:引用newAct=newQAction(QIcon(":/imagess/
3、new.png"),tr("&New"),this); newAct->setShortcut(tr("Ctrl+N")); newAct->setStatusTip(tr("Createanewfile")); connect(newAct,SIGNAL(triggered()),this,SLOT(newFile())); openAct=newQAction(QIcon(":/imagess/open.png"),tr("&Open..."),this); openAct->setShortcut(tr("Ctrl+O"));
4、 openAct->setStatusTip(tr("Openanexistingfile")); connect(openAct,SIGNAL(triggered()),this,SLOT(open())); saveAct=newQAction(QIcon(":/imagess/save.png"),tr("&Save"),this); saveAct->setShortcut(tr("Ctrl+S")); saveAct->setStatusTip(tr("Savethedocumenttodisk")); connect(
5、saveAct,SIGNAL(triggered()),this,SLOT(save())); saveAsAct=newQAction(tr("Save&As..."),this); saveAsAct->setStatusTip(tr("Savethedocumentunderanewname")); connect(saveAsAct,SIGNAL(triggered()),this,SLOT(saveAs())); exitAct=newQAction(tr("E&xit"),this); exitAct->setShortc
6、ut(tr("Ctrl+Q")); exitAct->setStatusTip(tr("Exittheapplication")); connect(exitAct,SIGNAL(triggered()),this,SLOT(close())); cutAct=newQAction(QIcon(":/imagess/cut.png"),tr("Cu&t"),this); cutAct->setShortcut(tr("Ctrl+X")); cutAct->setStatusTip(tr("Cutthecurrentselection'
7、scontentstothe" "clipboard")); connect(cutAct,SIGNAL(triggered()),textEdit,SLOT(cut())); copyAct=newQAction(QIcon(":/imagess/copy.png"),tr("&Copy"),this); copyAct->setShortcut(tr("Ctrl+C")); copyAct->setStatusTip(tr("Copythecurrentselection'scontentstothe"
8、