Important: Please read the Qt Code of Conduct -

Need a Little Help Drawing On Screen [Qt]

  • I have already read all the of examples etc on this topic and I am still confused.

    I'm working on a project, the general idea is I need to give the user a space within a layout when they can click and the program will use the x & y coordinates of those clicks in several calculations. The program must then draw in that same area the shapes generated by those calculations. I am doing this through visual studios as required by my professor.

    Right now I have constructed my layout and have placed a QWidget in the area I wish to use. I have attempted to use QPainter with the following section in an attempt to just draw a line and figure out how to use it. I am having a lot of trouble here and could use some help. Any incite into how to use QPainter or accomplish this with a better widget would be greatly appreciated.

    @ middle_box = new QGroupBox(tr("Middle Box"));
    QHBoxLayout *middle_layout = new QHBoxLayout();
    QGL_1 = new QWidget();
    QPainter paint(QGL_1);
    button_2 = new QPushButton(tr("M2 Button"));
    button_3 = new QPushButton(tr("M3 Button"));
    button_4 = new QPushButton(tr("M4 Button"));

    Thank you in advance for the help.

  • QPainter allows you to draw to any QPaintDevice subclass (typically QPixmap and QWidget). The most common usecase is to subclass QWidget, to reimplement the paintEvent() method and to use a QPainter to do custom painting.

    Be aware that you cannot use QPainter to draw on a widget outside of its paintEvent() method!

    Reimplementing various *Event() methods is "quite common": in Qt, and is amongst others used for handling mouse events.

    The "programming tutorial":, the "QWidget": and "QPainter": documentation as well as the "examples": and freely available books like "C++ GUI Programming with Qt 4": are a good place to start reading.

Log in to reply