Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Create An object that draw a circle (QPainter)
QtWS25 Last Chance

Create An object that draw a circle (QPainter)

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • yczoY Offline
    yczoY Offline
    yczo
    wrote on last edited by
    #1

    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);
    }

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      @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.

      1 Reply Last reply
      0
      • yczoY Offline
        yczoY Offline
        yczo
        wrote on last edited by
        #3

        Thank You very much, that works!

        mrjjM 1 Reply Last reply
        1
        • yczoY yczo

          Thank You very much, that works!

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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 :)

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved