2

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

user3676560
  • 151
  • 2
  • 15

2 Answers2

3

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.

Nithish
  • 1,580
  • 12
  • 21
0

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:

  1. Write Q_OBJECT macro in the beginning of your MainWindow definition (class containing a slot)
  2. Write on_pushButton_clicked in private slots: section of your class (instead of normal private: section)
  3. Write call to 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.
  4. Rerun qmake if you're using Visual Studio.

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

Predelnik
  • 5,066
  • 2
  • 24
  • 36
  • there is a Build-Problem: expected constructor, destructor, or type conversion before'(' token. – user3676560 Apr 15 '15 at 10:56
  • 2
    @user3676560 it's hard to tell what is the problem exactly without the full code, but it's not advisable to post full code is here, so I advice you to try to build some examples from Qt first because it may also be a problem with project includes/configuration. – Predelnik Apr 15 '15 at 10:59