Couldn't use QPainter in a function.
-
Hi All,
I am a beginner QT/C++ Programmer tasked to create an application to graph values.
The goal is to draw a graph (using QPainter) based on values from an array, upon pressing a button, and once that same button is pressed again, the graph is redrawn but with empty values (erasing the previous plot).
I receive errors when using an 'emit' signal command.The following is the way my code is structured:
header.h
#pragma once #include <QWidget> #include <QPainter> #include <QApplication> #include <QPushButton> #include <QLabel> #include <QIntValidator> #include <Qt> #include <QtDebug> #include <QLineEdit> #include <QString> #include <QFont> #include <QGraphicsScene> #include <QVector> #include <QDesktopWidget> class CGui : public QWidget { Q_OBJECT public: CGui(QWidget *parent = 0); private slots: void clickCnts(); void clickSngl(); private: QLabel *rltxt; QLabel *imtxt; QLabel *strttxt; QLabel *stoptxt; QLabel *lG1yaxis1; QLabel *lG1yaxis2; QLabel *lG1yaxis3; QLabel *lG1yaxis4; QLabel *lG1yaxis5; QLabel *lG1xaxis1; QLabel *lG1xaxis2; QLabel *lG1xaxis3; QLabel *lG2yaxis1; QLabel *lG2yaxis2; QLabel *lG2yaxis3; QLabel *lG2yaxis4; QLabel *lG2yaxis5; QLabel *lG2xaxis1; QLabel *lG2xaxis2; QLabel *lG2xaxis3; QLabel *lG2xaxis4; QLabel *lG2zaxis1; QLabel *lG2zaxis2; QLabel *lG2zaxis3; QLineEdit *StrtEdit; QLineEdit *StopEdit; QPushButton *CntsBtn; QPushButton *SnglBtn; protected: void paintEvent(QPaintEvent *e); void drawPlot(QPainter *painter); void drawEmptyGraphs(QPainter *painter); };
header.cpp
/* Code Removed Here initializing arrays and variables, as well as #includes */ CGui::CGui(QWidget *parent) : QWidget(parent) { CntsBtn = new QPushButton("CONTINUOUS", this); CntsBtn->setFont(font1); CntsBtn->setStyleSheet("background-color:rgb(38,208,124); border:none; color:white;"); CntsBtn->setGeometry(scrW*0.50625, scrH*0.3125, scrW*0.24125, scrH*0.1041666667); /* Additional Code removed here for Initializing Axis labels */ connect(CntsBtn, &QPushButton::clicked, this, &CGui::clickCnts); } void CGui::paintEvent(QPaintEvent *e) { Q_UNUSED(e); QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); drawPlot(&painter); drawEmptyGraphs(&painter); } void CGui::drawEmptyGraphs(QPainter *gpaintTool) { /* EMPTY GRAPHS DRAWN HERE */ } void CGui::drawPlot(QPainter *paintTool) { /* LINES ON GRAPH PLOTTED HERE */ } void CGui::clickCnts(){ if (StartFlag == 0) { StartFlag = 1; qDebug() << "CGui: Processing Started..."; CntsBtn->setText("STOP"); /* Axis Values replaced here based on min and max*/ emit drawPlot(); //this line is an error } else { StartFlag = 0; qDebug() << "CGui: Processing Stopped!"; CntsBtn->setText("CONTINUOUS"); emit drawEmptyGraphs(); //this line is an error } }
I am pretty sure that those emit lines are where the problems is. I tried creating a new QPainter object in the function, but the QPainter may only be used in QPaintEvent. What could be a solution?
Thank you for your time in reading all this.
Kind regards,
Shoaib -
@Shoaib-Muhammad said in Couldn't use QPainter in a function.:
emit drawEmptyGraphs(); //this line is an error
drawEmptyGraphs() is not declared as signal!
Also, signals are not implemented by you as developer, you only declare them! See https://doc.qt.io/qt-5/signalsandslots.html
But I don't know why you want to have signal here at all: why not just call drawEmptyGraphs() as a normal method?
If you want to trigger a paintEvent then call update() on that widget. -
Hi jsulm,
Thank you for the reply.
I tried just calling drawEmptyGraphs(); but I had an error stating a slot and signal mismatch.
Could you please provide an example I could base on? I am a bit lost right now.
Kind regards,
Shoaib -
@Shoaib-Muhammad said in Couldn't use QPainter in a function.:
I tried just calling drawEmptyGraphs(); but I had an error stating a slot and signal mismatch.
Please show your code. Calling a method does not have anything to do with signals/slots.
void CGui::clickCnts(){ if (StartFlag == 0) { StartFlag = 1; qDebug() << "CGui: Processing Started..."; CntsBtn->setText("STOP"); /* Axis Values replaced here based on min and max*/ drawPlot(); } else { StartFlag = 0; qDebug() << "CGui: Processing Stopped!"; CntsBtn->setText("CONTINUOUS"); drawEmptyGraphs(); } }