qt应用实例

本文详细介绍了Qt应用的各种实用技巧,包括窗体关闭判断、文件对话框操作、创建Actions、主菜单与工具栏构建、使用QSettings保存配置、QMessageBox对话框、通用对话框本地化、静态链接Qt库、源代码中直接使用中文、处理数据库连接问题、创建及使用DLL、启动外部程序、报表打印、系统托盘图标显示、日志输出到文件、将图像编译到程序中、制作不规则形状窗体、处理数据库连接删除错误以及Windows下读取串口信息。通过这些实例,开发者能更好地掌握Qt开发中的常见问题和解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值