当前位置:网站首页>[QT] Qt project demo: data is displayed on the ui interface, double-click the mouse to display specific information in a pop-up window
[QT] Qt project demo: data is displayed on the ui interface, double-click the mouse to display specific information in a pop-up window
2022-08-03 16:04: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);
//Whether the last line fills the remaining space
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("System boot status message"));
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("点迹/track information content"));
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("BITStatus information message"));
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("Sonic information message"));
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("Measure the feedback data of the bottom control box of the boat"));
//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

四、项目下载地址
边栏推荐
- NodeJs - cross domain
- 5 v 8.4 v1A charging current charging management IC
- 深度学习——安装CUDA以及CUDNN实现tensorflow的GPU运行
- 开源一夏 | 阿里云物联网平台之极速体验
- Leetcode76. 最小覆盖子串
- 泰山OFFICE技术讲座:段落边框的绘制难点在哪里?
- Three key expectations for the crypto market in August Price moves north?Still expected to be in turmoil
- Js array method is summarized
- 参与便有奖,《新程序员》杂志福利来袭!
- MATLAB gcf图窗保存图像,黑色背景/透明背景
猜你喜欢

【码蹄集新手村600题】将一个函数定义宏

Daily practice------There are 10 numbers that are required to be output from large to small by selection method

Awesome!Coroutines are finally here!Thread is about to be in the past

js数组方法总结

【QT】Qt 给已经开发好的程序快速封装成动态库

新一代网状网协议T-Mesh无线通信技术优势介绍

MySQL中的基数是啥?

ECCV 2022 | 基于关系查询的时序动作检测方法

【899. 有序队列】

Small Tools(4) 整合Seata1.5.2分布式事务
随机推荐
ffplay视频播放原理分析
AI也有健忘症?英国41岁教授专访:解决灾难性遗忘
破解数字化转型困局,企业分析协同场景案例解析
不安装运行时运行.NET程序
I am doing open source in Didi
在 360 度绩效评估中应该问的 20 个问题
mysql delete 执行报错:You can‘t specify target table ‘doctor_info‘ for update in FROM clause
How to start an NFT collection
5v充8.4v1A电流充电管理ic
How to play deep paging with hundreds of millions of data?Compatible with MySQL + ES + MongoDB
With a single operation, I improved the SQL execution efficiency by 10,000,000 times!
每日练习------有10个数字要求分别用选择法从大到小输出
我在滴滴做开源
【899. 有序队列】
基于DMS的数仓智能运维服务,知多少?
出海季,互联网出海锦囊之本地化
自定SvgIcon公用组件
DC-DC 2C(40W/30W) JD6606SX2退功率应用
AWS中国区SDN Connector
Three key expectations for the crypto market in August Price moves north?Still expected to be in turmoil