Reputation: 151
i have an UI with two buttons. My first button read an xml file. The second button should create an window an show a circle.
I have a mainwindow.h and a circle.h. Now i want to start my circle function with a pushbutton click.
My circle function:
void circle::paintEvent(QPaintEvent *e)
{
Q_UNUSED(e);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(QPen(QBrush("#888"), 1));
painter.setBrush(QBrush(QColor("#888")));
qDebug() << "\r\nxarray : " <<"test";
for(int z = 0; z < 50; z++)
{
painter.drawEllipse(10, 10, 100, 100);
}
}
Atm i start it with:
circle::circle(QWidget *parent)
: QWidget(parent)
{}
But i want to start it with:
void MainWindow::on_pushButton_clicked(
{}
How can i start my function with the pushButton?
[I just started to learn qt/c++ so im a beginner]
greetings
Upvotes: 2
Views: 3036
Reputation: 1640
In the slot on_pushButton_clicked()
create an instance of the class circle
and call the required method through that instance.
EDIT:
I do not know what method you intend to call in the circle class. But assuming you have a function called myFunc()
, the code would be something like:
void MainWindow::on_pushButton_clicked()
{
circle* circleObj = new circle(this); // instance created
circleObj->myFunct(); // the necessary actions are done in this function
// connect the signals from the circle class if any to the required slots in MainWindow
connect(circle, SIGNAL(mySignal()), this, SLOT(mySlot());
}
Since you seem to be completely new to Qt looking at your comments, I highly recommend you go through the documentation and the awesome tutorials at the VoidRealms YouTube channel before proceeding further.
Upvotes: 3
Reputation: 5246
Qt uses signal and slot system for specifying actions triggered by the user. So if you specify function on_pushButton_clicked
you should do the following:
Q_OBJECT
macro in the beginning of your MainWindow
definition (class containing a slot)on_pushButton_clicked
in private slots:
section of your class (instead of normal private:
section)QObject::connect
somewhere (possibly in constructor) with the following syntax: connect (button, SIGNAL(clicked ()), this, SLOT(on_pushButton_clicked ()));
, read Qt manual for which signals are available.Specifically to do what you want in your case, you probably need to create some flag which will be set in on_pushButton_clicked
function and check for this flag in paintEvent
.
Additional info about signal and slots from Qt documentation: http://doc.qt.io/qt-5/signalsandslots.html
Upvotes: 0