当前位置:网站首页>【QT】Qt项目demo:数据在ui界面上显示,鼠标双击可弹窗显示具体信息
【QT】Qt项目demo:数据在ui界面上显示,鼠标双击可弹窗显示具体信息
2022-08-03 15:49:00 【Cappuccino-jay】
一、项目总览
二、运行结果
I、鼠标单击
II、鼠标双击
三、源文件代码
1、RecData.pro
#-------------------------------------------------
#
# Project created by QtCreator 2022-07-23T15:24:27
#
#-------------------------------------------------
QT += core gui sql
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = RecData
TEMPLATE = app
#TEMPLATE = lib
#DEFINES += RECDATA_LIBRARY
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG += c++11
SOURCES += \
main.cpp \
RecData.cpp \
showdata.cpp
HEADERS += \
RecData.h \
showdata.h
FORMS += \
RecData.ui \
showdata.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${
TARGET}/bin
else: unix:!android: target.path = /opt/$${
TARGET}/bin
!isEmpty(target.path): INSTALLS += target
2、RecData.h
#ifndef RECDATA_H
#define RECDATA_H
#include <QWidget>
#include <QStandardItemModel>
#include <QAbstractItemView>
#include <QPushButton>
#include <QTime>
#include <QSqlRecord>
#include <QDebug>
#include <QMessageBox>
#include <QMouseEvent>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>
namespace Ui {
class RecData;
}
class RecData : public QWidget
{
Q_OBJECT
public:
explicit RecData(QWidget *parent = nullptr);
~RecData();
private slots:
void on_tableView_doubleClicked(const QModelIndex &index);
void on_tableView_clicked(const QModelIndex &index);
private:
Ui::RecData *ui;
QModelIndex pindex;
QString pstr;
// QStandardItemModel *model;
};
#endif // RECDATA_H
3、RecData.cpp
#pragma execution_character_set("utf-8")
#include "RecData.h"
#include "ui_RecData.h"
#include "showdata.h"
#include "ui_showdata.h"
QStandardItemModel *model = nullptr;
RecData::RecData(QWidget *parent) :
QWidget(parent),
ui(new Ui::RecData)
{
ui->setupUi(this);
//QStandardItemModel *model = new QStandardItemModel();
model = new QStandardItemModel;
model->setHorizontalHeaderItem(0,new QStandardItem(QObject::tr("No")));
model->setHorizontalHeaderItem(1,new QStandardItem(QObject::tr("时间")));
model->setHorizontalHeaderItem(2,new QStandardItem(QObject::tr("实现方式")));
model->setHorizontalHeaderItem(3,new QStandardItem(QObject::tr("使用方式")));
model->setHorizontalHeaderItem(4,new QStandardItem(QObject::tr("频率")));
model->setHorizontalHeaderItem(5,new QStandardItem(QObject::tr("主题")));
model->setHorizontalHeaderItem(6,new QStandardItem(QObject::tr("描述")));
ui->tableView->setModel(model);
ui->tableView->horizontalHeader()->setDefaultAlignment(Qt::AlignCenter);
ui->tableView->setColumnWidth(6,300);
ui->tableView->verticalHeader()->hide();
// 行选择,选中整行
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
// 选择模式,单选
ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
//ui->tableView->setSelectionMode(QAbstractItemView::MultiSelection);
//隔行变色
ui->tableView->setAlternatingRowColors(true);
//根据内容自动调整列宽
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
//最后一行是否铺满剩余空间
ui->tableView->horizontalHeader()->setStretchLastSection(true);
//QString framedata = QString("[%1]:RX").arg(QTime::currentTime().toString("HH:mm:ss.zzz"));
//model->setHorizontalHeaderItem(1,QStandardItem(framedata));
QDateTime current_date_time = QDateTime::currentDateTime(); //获取当前系统时间
QString current_date = current_date_time.toString("yyyy/MM/dd hh:mm:ss"); //时间格式
qDebug() << current_date;
model->setItem(0,0,new QStandardItem("1"));
model->setItem(0,1, new QStandardItem("2022/07/23"));
model->setItem(0,2, new QStandardItem("publish"));
model->setItem(0,3, new QStandardItem("subscribe"));
model->setItem(0,4, new QStandardItem("1HZ"));
model->setItem(0,5, new QStandardItem("/10001901/2/1"));
model->setItem(0,6, new QStandardItem("系统开机状态报文"));
model->setItem(1,0,new QStandardItem("2"));
model->setItem(1,1, new QStandardItem("2022/07/23"));
model->setItem(1,2, new QStandardItem("publish"));
model->setItem(1,3, new QStandardItem("subscribe"));
model->setItem(1,4, new QStandardItem("1HZ"));
model->setItem(1,5, new QStandardItem("/10001901/2/2"));
model->setItem(1,6, new QStandardItem("点迹/航迹信息内容"));
model->setItem(2,0,new QStandardItem("3"));
model->setItem(2,1, new QStandardItem("2022/07/25"));
model->setItem(2,2, new QStandardItem("publish"));
model->setItem(2,3, new QStandardItem("subscribe"));
model->setItem(2,4, new QStandardItem("1HZ"));
model->setItem(2,5, new QStandardItem("/10001901/2/3"));
model->setItem(2,6, new QStandardItem("BIT状态信息消息"));
model->setItem(3,0,new QStandardItem("4"));
model->setItem(3,1, new QStandardItem("2022/07/26"));
model->setItem(3,2, new QStandardItem("publish"));
model->setItem(3,3, new QStandardItem("subscribe"));
model->setItem(3,4, new QStandardItem("2HZ"));
model->setItem(3,5, new QStandardItem("/10001801/3/1"));
model->setItem(3,6, new QStandardItem("声波信息消息"));
model->setItem(4,0,new QStandardItem("5"));
model->setItem(4,1, new QStandardItem("2022/08/01"));
model->setItem(4,2, new QStandardItem("publish"));
model->setItem(4,3, new QStandardItem("subscribe"));
model->setItem(4,4, new QStandardItem("5HZ"));
model->setItem(4,5, new QStandardItem("/10002201/4/2"));
model->setItem(4,6, new QStandardItem("测量艇底层控制箱反馈数据"));
//connect(ui->tableView, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(on_tableView_doubleClicked(const QModelIndex &)));
//connect(ui->tableView, SIGNAL(clicked(const QModelIndex &)), this, SLOT(on_tableView_doubleClicked(const QModelIndex &)));
}
RecData::~RecData()
{
delete ui;
}
void RecData::on_tableView_doubleClicked(const QModelIndex &index)
{
showData *dataWindow = new showData;
dataWindow->setWindowTitle("showData");
dataWindow->show();
QModelIndex row = ui->tableView->currentIndex();
qDebug() << row;
QStringList list;
for(int i = 0; i < model->columnCount(); i++)
{
pindex = model->index(index.row(),i);
pstr = model->data(pindex).toString();
list << pstr;
qDebug() << "data is: +" << pstr;
}
dataWindow->setData(list);
}
void RecData::on_tableView_clicked(const QModelIndex &index)
{
ui->plainTextEdit->clear();
for(int i = 0; i < model->columnCount(); i++)
{
pindex = model->index(index.row(),i);
pstr = model->data(pindex).toString();
ui->plainTextEdit->appendPlainText(pstr);
qDebug() << "data is: +" << pstr;
}
}
4、showdata.h
#ifndef SHOWDATA_H
#define SHOWDATA_H
#include <QWidget>
namespace Ui {
class showData;
}
class showData : public QWidget
{
Q_OBJECT
public:
explicit showData(QWidget *parent = nullptr);
~showData();
void setData(QStringList &list);
private:
Ui::showData *ui;
};
#endif // SHOWDATA_H
5、showdata.cpp
#include "showdata.h"
#include "RecData.h"
#include "ui_showdata.h"
extern RecData *model;
showData::showData(QWidget *parent) :
QWidget(parent),
ui(new Ui::showData)
{
ui->setupUi(this);
}
showData::~showData()
{
delete ui;
}
void showData::setData(QStringList &list)
{
for(const auto &iter : list)
{
ui->plainTextEdit->appendPlainText(iter);
}
}
6、main.cpp
#include "RecData.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
RecData w;
w.show();
return a.exec();
}
7、RecData.ui
8、showdata.ui
四、项目下载地址
边栏推荐
- ffplay视频播放原理分析
- Convex Optimization of Optimal Power Flow (OPF) in Microgrids and DC Grids (Matlab Code Implementation)
- 您的移动端app安全吗
- 2021年12月电子学会图形化四级编程题解析含答案:棕熊大战
- 请问下阿里云全托管flink能执行两条flink sql命令么?
- 【周报】2022年7月31日
- 用户侧有什么办法可以自检hologres单表占用内存具体是元数据、计算、缓存的使用情况?
- Js array method is summarized
- 2021年12月电子学会图形化四级编程题解析含答案:新冠疫苗接种系统
- Taurus.MVC WebAPI 入门开发教程1:框架下载环境配置与运行(含系列目录)。
猜你喜欢
破解数字化转型困局,企业分析协同场景案例解析
Essentially a database data recovery 】 【 database cannot read data recovery case
一通骚操作,我把SQL执行效率提高了10000000倍!
How to get the 2 d space prior to ViT?UMA & Hong Kong institute of technology & ali SP - ViT, study for visual Transformer 2 d space prior knowledge!.
人脸识别损失函数的汇总 | Pytorch版本实现
新一代网状网协议T-Mesh无线通信技术优势介绍
【数据库数据恢复】SqlServer数据库无法读取的数据恢复案例
2021年12月电子学会图形化三级编程题解析含答案:跳高比赛
Fortinet产品导入AWS AMI操作文档
红蓝对抗经验分享:CS免杀姿势
随机推荐
【Unity入门计划】基本概念(8)-瓦片地图 TileMap 02
Flink作业调度详解
How Navicat connects to MySQL on a remote server
ReentrantLock详解
Phaser(二):小恐龙跑酷游戏
破解数字化转型困局,企业分析协同场景案例解析
MySQL性能优化_小表驱动大表
devops-3:Jenkins增加静态节点
分布式系统与微服务的区别
13、OOM模拟
技术干货|如何将 Pulsar 数据快速且无缝接入 Apache Doris
16 【过渡 动画】
ffplay视频播放原理分析
2021年12月电子学会图形化一级编程题解析含答案:放学
微电网和直流电网中最优潮流(OPF)的凸优化(Matlab代码实现)
方舟开服教程win
深入浅出Flask PIN
AWS中国区SDN Connector
土耳其国防部:联合协调中心将对首艘乌克兰粮船进行安全检查
Essentially a database data recovery 】 【 database cannot read data recovery case