Create An object that draw a circle (QPainter)



  • Hello all, I would like to create a circles matrix in MainWindow. With this purpose, I want to create an objet that draws a circle in MainWindow. I don't reach. Please can somebody help me to fix my code?

    Thanks in advance

    (ampel.h)
    //*********************************************
    #ifndef AMPEL_H
    #define AMPEL_H
    #include <QWidget>

    class ampel : public QWidget
    {
    public:
    explicit ampel(QWidget *parent = 0);
    protected:
    void paintEvent(QPaintEvent *e);
    private:
    int x;
    void doPainting();

    };
    #endif // AMPEL_H
    //***********************************************

    (ampel.cpp)
    //***********************************************
    #include "ampel.h"
    #include <QPainter>
    //#include<mainwindow.h>

    ampel::ampel(QWidget parent): QWidget(parent)
    {
    x=0;
    }
    //
    **********************************************
    void ampel::paintEvent(QPaintEvent *e){
    Q_UNUSED(e);

    doPainting();
    

    }
    //***********************************************
    void ampel::doPainting(){
    QPainter dc(this);
    QBrush pintada(Qt::green, Qt::SolidPattern);
    pintada.setColor(Qt::red);
    dc.setRenderHint(QPainter::Antialiasing,true);
    dc.setPen(QPen(Qt::black,3, Qt::DashLine, Qt::RoundCap));
    dc.setBrush(QBrush(pintada));// QBrush(Qt::green, Qt::SolidPattern)
    dc.drawEllipse(x,x,25,25);
    }
    //*********************************************

    (MainWindow.h)
    //**********************************************
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    #include "ampel.h"

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private:

    Ui::MainWindow *ui;
    

    };

    #endif // MAINWINDOW_H
    //*******************************************

    (MainWindow.cpp)
    //*******************************************
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    this->setFixedSize(1024,768);
    ampel a(this);
    }


  • Qt Champions 2016

    @yczo said:
    Hi
    Code looks fine But

    ampel a(this);

    this is a local variable and it will be deleted !
    as soon as constructor is fully run.
    so here
    ampel a(this);
    } <<--- here a is deleted as it runs out of scope.

    You must make it using new
    ampel *a=new ampel (this);
    ampel->resize(100,100); // set some size
    ampel->show(); // ask it to be shown
    setCentralWidget(ampel); // insert into mainwindows central widget.
    Mainwindow is special as it has a central place holder widget.



  • Thank You very much, that works!


  • Qt Champions 2016

    @yczo
    super.
    If you want to make it follow size of mainwindow , you can insert
    layout also. But mainwindow to fixed size so you seem not to need it :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.