如何将数组的值传递到类Qt 5.0 C++中

How to pass values of an array into a class Qt 5.0 C++

本文关键字:Qt C++ 数组 值传      更新时间:2023-10-16

我对Qt编程和面向对象编程相当陌生,我有一些C++知识,主要使用微控制器来实现简单的控制电路和自动化系统的硬件接口。

我正在尝试使用Qt创建一个GUI,以显示从存储在数组中的x-y点绘制的线,并能够使用单独的线程更新数组中的值。

我目前有一个非常简单的程序从数组中绘制线条,但我不知道如何从main.cpp或另一个线程将更新的值传递给这个数组。

我附上了我的代码如下。

// painterdialog.h
#ifndef PAINTERDIALOG_H
#define PAINTERDIALOG_H
#include <QDialog>
#include <QPainter>
namespace Ui {
class PainterDialog;
}
class PainterDialog : public QDialog
{
Q_OBJECT
public:
explicit PainterDialog(QWidget *parent = 0);
~PainterDialog();
private:
Ui::PainterDialog *ui;
protected:
void paintEvent(QPaintEvent *e);
};
#endif // PAINTERDIALOG_H

下方的main.cpp

#include "painterdialog.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
PainterDialog w;
w.show();
return a.exec();
}

下方的painterdialog.cpp

#include "painterdialog.h"
#include "ui_painterdialog.h"
PainterDialog::PainterDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::PainterDialog)
{
ui->setupUi(this);
}
PainterDialog::~PainterDialog()
{
delete ui;
}
void PainterDialog:: paintEvent(QPaintEvent *e)
{
int xVal[12] = {0,1,2,4,0,5,8,0,9,10,30,9999};
int yVal[12] = {0,1,2,5,0,7,7,0,8,20,10,9999};
QPainter MyPainter(this);
QPen PointPen(Qt::red);
PointPen.setWidth(5);
QPen LinePen(Qt::green);
LinePen.setWidth(2);
QPoint p1;
QPoint p2;
for(int x = 0; x<12 ; x++)
{
p1.setX(xVal[x]*10);
p1.setY(yVal[x]*10);
p2.setX(xVal[x+1]*10);
p2.setY(yVal[x+1]*10);
MyPainter.setPen(PointPen);
MyPainter.drawPoint(p1);
MyPainter.drawPoint(p2);
MyPainter.setPen(LinePen);
if(xVal[x]&&yVal[x] != 0 && xVal[x+1]&&yVal[x+1] !=0)//draw a connecting lines to points not going to the origin (infinity)
{
MyPainter.drawLine(p1, p2);
}
if(xVal[x+2]==9999)
{
break;
}
}

QPen LinePen2(Qt::black);
LinePen2.setStyle( Qt::DashDotLine );
LinePen2.setWidth(3);
MyPainter.setPen(LinePen2);
MyPainter.drawLine(QPoint(300,100), QPoint(100,200));
}

我想使用另一个过程来更新数组"xVal"answers"yVal"中的值,我想我需要创建一个指向数组的指针,我会不断更新值,并将指针传递给painter对话框类。。。

我看了一些教程,花了一些时间在各种网站上寻求帮助,但到目前为止一无所获。

如果有人能帮忙,将不胜感激

似乎是在混合术语"进程"answers"线程"。如果你的意思是"线程",那么我建议你编辑你的问题,因为线程之间的通信比进程之间的通信更容易编程。

我认为在您的案例中有两个主要概念是QThread类和信号/插槽机制。QThread用于通过以太网读取x/y数据,信号/插槽用于通知数据已更新并应重新绘制。

#include <QThread>
#include <QList>
#include <QPoint>
class DataFetcher : public QObject
{
Q_OBJECT
signals:
void dataChanged(QList<QPoint> data);
public slots:
void startFetching() {
//here you fill your data batch and pass it to PrinterDialog in GUI thread
/*cycle over data batches*/ {
QList<QPoint> data;
/*cycle over point in batch*/ {
QPoint p = ...; // take next point from your laser
data << p;
}
emit dataChanged(data);
}
}
};

在main.cpp中:

#include "painterdialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
PainterDialog w;
w.show();
DataFetcher df;
QThread fetcherThread;
//to execute df's slots in background thread
df.moveToThread(&fetcherThread);
//you'll need onDataChanged(QList<QPoint>) slot method and startDataFetching() signal in your Painter dialog
//wire up communication
QObject::connect(&df, &DataFetcher::dataChanged, &w, &PainterDialog::onDataChanged);
QObject::connect(&w, PainterDialog::startDataFetching, &df, &DataFetcher::startFetching);
//start background thread
fetcherThread.start();
//start GUI event loop
return a.exec();
//tell background thread that we are quiting
fetcherThread.quit();
//... and wait until background thread will finalize
fetcherThread.join();
}    

您不必担心删除QList,只要您按值传递它们,而不是按指针/引用传递它们。QList实际上不过是具有引用计数的指针(Qt的隐式数据共享)。