首页 > 其他分享 >Qt实现扇形图(2)

Qt实现扇形图(2)

时间:2023-02-21 14:34:40浏览次数:40  
标签:Qt 实现 tr fileName 扇形 file model QModelIndex row


#include "pieview.h"
#include "mainwindow.h"

#include <QtWidgets>

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QMenu *fileMenu = new QMenu(tr("&File"), this);
QAction *openAction = fileMenu->addAction(tr("&Open..."));
openAction->setShortcuts(QKeySequence::Open);
QAction *saveAction = fileMenu->addAction(tr("&Save As..."));
saveAction->setShortcuts(QKeySequence::SaveAs);
QAction *quitAction = fileMenu->addAction(tr("E&xit"));
quitAction->setShortcuts(QKeySequence::Quit);

setupModel();
setupViews();

connect(openAction, &QAction::triggered, this, &MainWindow::openFile);
connect(saveAction, &QAction::triggered, this, &MainWindow::saveFile);
connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);

menuBar()->addMenu(fileMenu);
statusBar();

loadFile(":/Charts/qtdata.cht");

setWindowTitle(tr("Chart"));
resize(870, 550);
}

void MainWindow::setupModel()
{
model = new QStandardItemModel(8, 2, this);
model->setHeaderData(0, Qt::Horizontal, tr("Label"));
model->setHeaderData(1, Qt::Horizontal, tr("Quantity"));
}

void MainWindow::setupViews()
{
QSplitter *splitter = new QSplitter;
QTableView *table = new QTableView;
pieChart = new PieView;
splitter->addWidget(table);
splitter->addWidget(pieChart);
splitter->setStretchFactor(0, 0);
splitter->setStretchFactor(1, 1);

table->setModel(model);
pieChart->setModel(model);

QItemSelectionModel *selectionModel = new QItemSelectionModel(model);
table->setSelectionModel(selectionModel);
pieChart->setSelectionModel(selectionModel);

QHeaderView *headerView = table->horizontalHeader();
headerView->setStretchLastSection(true);

setCentralWidget(splitter);
}

void MainWindow::openFile()
{
const QString fileName =
QFileDialog::getOpenFileName(this, tr("Choose a data file"), "", "*.cht");
if (!fileName.isEmpty())
loadFile(fileName);
}

void MainWindow::loadFile(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QFile::ReadOnly | QFile::Text))
return;

QTextStream stream(&file);

model->removeRows(0, model->rowCount(QModelIndex()), QModelIndex());

int row = 0;
while (!stream.atEnd()) {
const QString line = stream.readLine();
if (!line.isEmpty()) {
model->insertRows(row, 1, QModelIndex());

const QStringList pieces = line.split(',', QString::SkipEmptyParts);
if (pieces.size() < 3)
continue;
model->setData(model->index(row, 0, QModelIndex()),
pieces.value(0));
model->setData(model->index(row, 1, QModelIndex()),
pieces.value(1));
model->setData(model->index(row, 0, QModelIndex()),
QColor(pieces.value(2)), Qt::DecorationRole);
row++;
}
};

file.close();
statusBar()->showMessage(tr("Loaded %1").arg(fileName), 2000);
}

void MainWindow::saveFile()
{
QString fileName = QFileDialog::getSaveFileName(this,
tr("Save file as"), "", "*.cht");

if (fileName.isEmpty())
return;

QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text))
return;

QTextStream stream(&file);
for (int row = 0; row < model->rowCount(QModelIndex()); ++row) {

QStringList pieces;

pieces.append(model->data(model->index(row, 0, QModelIndex()),
Qt::DisplayRole).toString());
pieces.append(model->data(model->index(row, 1, QModelIndex()),
Qt::DisplayRole).toString());
pieces.append(model->data(model->index(row, 0, QModelIndex()),
Qt::DecorationRole).toString());

stream << pieces.join(',') << "\n";
}

file.close();
statusBar()->showMessage(tr("Saved %1").arg(fileName), 2000);
}

Qt实现扇形图(2)_QFile

标签:Qt,实现,tr,fileName,扇形,file,model,QModelIndex,row
From: https://blog.51cto.com/u_15515702/6076565

相关文章

  • 基于CANoe和Visual Studio实现Classic 和Adaptive AUTOSAR应用层调试
    “转载自维克多汽车技术(上海)有限公司,作者VectorChina”伴随“软件化”和“敏捷化”的推进,从基于域(Domain)架构到基于区域(Zonal)架构的发展,亦或OEM成立软件中心参与软件开发......
  • 4.4可变长数组类实现
    #include<iostream>#include<cstring>#include<algorithm>usingnamespacestd;classcarray{intsize;int*ptr;//指向数组的指针public:carray(......
  • Qt音视频开发15-动态切换解码内核的设计
    一、前言动态切换解码内核这个需求也是源自客户的真实需求,既然是动态切换,那肯定是运行期间切换,而不是通过改变标志位重新编译程序来切换,最开始做的就是这种方式,这样就是实......
  • C# 利用FluentFTP实现FTP上传下载功能
    FTP作为日常工作学习中,非常重要的一个文件传输存储空间,想必大家都非常的熟悉了,那么如何快速的实现文件的上传下载功能呢,本文以一个简单的小例子,简述如何通过FluentFTP实现......
  • Excel文件 利用MySQL/Python 实现自动处理数据的功能
    目录问题描述:解决方案:一、SQL查询二、SQL、python处理三、python处理四、优化python处理1.手动执行代码2.开机自动执行代码对比四种方案:总结:问题描述:在没有服务器存储数......
  • C++面对对象:实现complex类
    1#ifndef__MYCOMPLEX_H__2#define__MYCOMPLEX_H__34classcomplex;5complex&__doapl(complex*,constcomplex&);//友元可以在类外声明6compl......
  • python、js实现WGS84、高德(火星)、百度坐标转换
    在日常工作学习中常会涉及到WGS84、高德(火星/谷歌)、百度三种空间坐标系的坐标转换,本文将通过python、js两种语言实现坐标系的转换。坐标系说明:wgs84:为一种大地......
  • Metux 实现单例进程
    WPF单例 publicabstractclassSingletonApplication:Application{privateMutex_singleTonApp=null;protectedSingletonApplicatio......
  • Filter_案例2_过滤敏感词汇_实现与Listener_概述
    Filter_案例2_过滤敏感词汇_实现   packagehf.xueqiang.web.filter;importjavax.servlet.*;importjavax.servlet.annotation.Web......
  • 多线程并发(二):聊聊AQS中的共享锁实现原理
    在上一篇文章多线程并发(一)中我们通过acquire()详细地分析了AQS中的独占锁的获取流程,提到独占锁,自然少不了共享锁,所以我们这边文章就以AQS中的acquireShared()方法为例,......