当前位置:网站首页>[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
四、项目下载地址
边栏推荐
猜你喜欢
随机推荐
Ark server opening tutorial win
How Navicat connects to MySQL on a remote server
常见分布式理论(CAP、BASE)和一致性协议(Gosssip、Raft)
ModelWhale 云端运行 WRF 中尺度数值气象模式,随时随地即开即用的一体化工作流
30W 2C(JD6606S + FP6652X2)BOM
ReentrantReadWriteLock详解
【Unity入门计划】基本概念(7)-Input Manager&Input类
如何选择合适的损失函数,请看......
AWS China SDN Connector
不安装运行时运行.NET程序
Essentially a database data recovery 】 【 database cannot read data recovery case
How to start an NFT collection
[Code Hoof Set Novice Village 600 Questions] Define a function as a macro
cnpm 安装成功后提示不是内部和外部命令,也不是可运行的命令解决方案
Daily practice------There are 10 numbers that are required to be output from large to small by selection method
ECCV 2022 | 基于关系查询的时序动作检测方法
托尔斯泰:生活中只有两种不幸
spark入门学习-1
1、实例开启无锁表结构变更以后,在任务编排中通过“单实例SQL”节点进行的结构变更,是优先采用无锁表
MySQL性能优化_小表驱动大表