#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QCloseEvent>
#include "qwdialogsize.h"
void MainWindow::closeEvent(QCloseEvent *event)
{ //窗口关闭时询问是否退出
QMessageBox::StandardButton result=QMessageBox::question(this, "确认", "确定要退出本程序吗?",
QMessageBox::Yes|QMessageBox::No |QMessageBox::Cancel,
QMessageBox::No);
if (result==QMessageBox::Yes)
event->accept();
else
event->ignore();
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
theModel = new QStandardItemModel(10,5,this); //创建数据模型
theSelection = new QItemSelectionModel(theModel);//Item选择模型
connect(theSelection,SIGNAL(currentChanged(QModelIndex,QModelIndex)),
this,SLOT(on_currentChanged(QModelIndex,QModelIndex)));
//为tableView设置数据模型
ui->tableView->setModel(theModel); //设置数据模型
ui->tableView->setSelectionModel(theSelection);//设置选择模型
setCentralWidget(ui->tableView); //
//创建状态栏组件
LabCellPos = new QLabel("当前单元格:",this);
LabCellPos->setMinimumWidth(180);
LabCellPos->setAlignment(Qt::AlignHCenter);
LabCellText = new QLabel("单元格内容:",this);
LabCellText->setMinimumWidth(200);
ui->statusBar->addWidget(LabCellPos);
ui->statusBar->addWidget(LabCellText);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::setActLocateEnable(bool enable)
{
ui->actTab_Locate->setEnabled(enable);
}
//void MainWindow::selectACell(int row, int column)
//{
// QModelIndex index=theModel->index(row,column);
// theSelection->clearSelection();
// theSelection->setCurrentIndex(index,QItemSelectionModel::Select);
//}
void MainWindow::setACellText(int row, int column, QString text)
{//定位到单元格,并设置字符串
QModelIndex index=theModel->index(row,column);//获取模型索引
theSelection->clearSelection(); //清除现有选择
theSelection->setCurrentIndex(index,QItemSelectionModel::Select); //定位到单元格
theModel->setData(index,text,Qt::DisplayRole);//设置单元格字符串
}
void MainWindow::setDlgLocateNull()
{
dlgLocate=NULL;
}
void MainWindow::on_currentChanged(const QModelIndex ¤t, const QModelIndex &previous)
{
if (current.isValid()) //当前模型索引有效
{
LabCellPos->setText(QString::asprintf("当前单元格:%d行,%d列",
current.row(),current.column())); //显示模型索引的行和列号
QStandardItem *aItem;
aItem=theModel->itemFromIndex(current); //从模型索引获得Item
this->LabCellText->setText("单元格内容:"+aItem->text()); //显示item的文字内容
// QFont font=aItem->font(); //获取item的字体
// ui->actFontBold->setChecked(font.bold()); //更新actFontBold的check状态
}
}
void MainWindow::on_actTab_SetSize_triggered()
{ //模态对话框,动态创建,用过后删除
QWDialogSize *dlgTableSize=new QWDialogSize(this); //创建对话框
// dlgTableSize->setAttribute(Qt::WA_DeleteOnClose);
//对话框关闭时自动删除对话框对象,用于不需要读取返回值的对话框
//如果需要获取对话框的返回值,不能设置该属性,可以在调用完对话框后删除对话框
Qt::WindowFlags flags=dlgTableSize->windowFlags();
dlgTableSize->setWindowFlags(flags | Qt::MSWindowsFixedSizeDialogHint); //设置对话框固定大小
dlgTableSize->setRowColumn(theModel->rowCount(),theModel->columnCount()); //对话框数据初始化
int ret=dlgTableSize->exec();// 以模态方式显示对话框,用户关闭对话框时返回 DialogCode值
if (ret==QDialog::Accepted) //OK键被按下,对话框关闭,若设置了setAttribute(Qt::WA_DeleteOnClose),对话框被释放,无法获得返回值
{ //OK键被按下,获取对话框上的输入,设置行数和列数
int cols=dlgTableSize->columnCount();
theModel->setColumnCount(cols);
int rows=dlgTableSize->rowCount();
theModel->setRowCount(rows);
}
delete dlgTableSize; //删除对话框
}
void MainWindow::on_actTab_SetHeader_triggered()
{//一次创建,多次调用,对话框关闭时只是隐藏
if (dlgSetHeaders==NULL) //如果对象没有被创建过,就创建对象
dlgSetHeaders = new QWDialogHeaders(this);
if (dlgSetHeaders->headerList().count()!=theModel->columnCount())
{//如果表头列数变化,重新初始化
QStringList strList;
for (int i=0;i<theModel->columnCount();i++)//获取现有的表头标题
strList.append(theModel->headerData(i,Qt::Horizontal,Qt::DisplayRole).toString());
dlgSetHeaders->setHeaderList(strList);//用于对话框初始化显示
}
int ret=dlgSetHeaders->exec();// 以模态方式显示对话框
if (ret==QDialog::Accepted) //OK键被按下
{
QStringList strList=dlgSetHeaders->headerList();//获取对话框上修改后的StringList
theModel->setHorizontalHeaderLabels(strList);// 设置模型的表头标题
}
}
void MainWindow::on_actTab_Locate_triggered()
{//创建 StayOnTop的对话框,对话框关闭时自动删除
//通过控制actTab_Locate的enable属性避免重复点击
ui->actTab_Locate->setEnabled(false);
dlgLocate = new QWDialogLocate(this); //创建对话框,传递指针
dlgLocate->setAttribute(Qt::WA_DeleteOnClose); //对话框关闭时自动删除对话框对象,用于不需要读取返回值的对话框
Qt::WindowFlags flags=dlgLocate->windowFlags(); //获取已有flags
//对话框设置为固定大小和StayOnTop
// dlgLocate->setWindowFlags(flags |Qt::MSWindowsFixedSizeDialogHint |Qt::WindowStaysOnTopHint); //设置对话框固定大小,StayOnTop
dlgLocate->setWindowFlags(flags | Qt::WindowStaysOnTopHint); //设置对话框固定大小,StayOnTop
//对话框初始化设置
dlgLocate->setSpinRange(theModel->rowCount(),theModel->columnCount());
QModelIndex curIndex=theSelection->currentIndex();
if (curIndex.isValid())
dlgLocate->setSpinValue(curIndex.row(),curIndex.column());
dlgLocate->show(); //非模态显示对话框
// this->show();
}
void MainWindow::on_tableView_clicked(const QModelIndex &index)
{//单击单元格时,将单元格的行号、列号设置到对话框上
if (dlgLocate!=NULL) //对话框存在
dlgLocate->setSpinValue(index.row(),index.column());
}
没有合适的资源?快使用搜索试试~ 我知道了~
QT自定义对话框及其调用应用程序示例
共37个文件
bmp:17个
h:8个
cpp:5个
0 下载量 30 浏览量
2024-01-16
09:55:29
上传
评论
收藏 22KB RAR 举报
温馨提示
在一个应用程序设计中,为了实现一些特定的功能,必须设计自定义对话框。自定义对话框的设计一般从QDialog继承,并且可以采用UI设计器可视化地设计对话框。对话框的调用一般包括创建对话框、传递数据给对话框、显示对话框获取输入、判断对话框单击按钮的返回类型、获取对话框输入数据等过程。 实例samp6_2详细介绍了这些原理。实例samp6_2的主窗口采用QTableView和QStandardItemModel、QItemSelectionModel构成一个通用的数据表格编辑器,设计了3个对话框,分别具有不同的功能,并且展示对话框不同调用方式的特点。
资源推荐
资源详情
资源评论
收起资源包目录
samp6_2CustomDialogs.rar (37个子文件)
samp6_2CustomDialogs
samp6_2.pro.user 25KB
qwdialoglocate.ui 3KB
qwdialogheaders.ui 5KB
mainwindow.h 2KB
ui_qwdialogheaders.h 5KB
mainwindow.cpp 7KB
qwdialogheaders.cpp 636B
ui_qwdialogsize.h 5KB
qwdialogheaders.h 503B
main.cpp 183B
mainwindow.ui 3KB
ui_qwdialoglocate.h 6KB
qwdialogsize.h 546B
ui_mainwindow.h 6KB
qwdialoglocate.cpp 2KB
res.qrc 326B
images
122.bmp 1KB
514.bmp 1KB
704.bmp 1KB
512.bmp 1KB
510.bmp 1KB
100.bmp 1KB
304.bmp 1KB
506.bmp 1KB
406.bmp 1KB
402.bmp 1KB
104.bmp 1KB
516.bmp 1KB
230.bmp 1KB
706.bmp 1KB
404.bmp 1KB
132.bmp 1KB
400.bmp 1KB
qwdialoglocate.h 848B
samp6_2.pro 633B
qwdialogsize.ui 4KB
qwdialogsize.cpp 779B
共 37 条
- 1
资源评论
冷凝女子
- 粉丝: 499
- 资源: 67
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功