Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6771639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:27:21+00:00 2026-05-26T15:27:21+00:00

I don’t have much experience with Qt and I am having trouble using QPainter

  • 0

I don’t have much experience with Qt and I am having trouble using QPainter.
I am trying to make a simple graphing widget which takes in a number of points and to create a QVector of QPoints, and then uses this vector to draw a polygon. However, nothing is appearing right now with my implementation. I am fairly certain that I have added the widget correctly to the window, as I can see the empty space it should occupy. This leads me to believe the problem to be in the graphing widget.
Any assistance is appreciated.

header:

//graph.h

#ifndef GRAPH_H
#define GRAPH_H

#include <QWidget>
#include <QPainter>
#include <QVector>

class Graph : public QWidget
{
    Q_OBJECT

public:
    Graph(QWidget *parent = 0);
    QSize minimumSizeHint() const;
    QSize maximumSizeHint() const;
    QSize sizeHint() const;

    void addPoint(int w, int h);
    void clearPoints();
    void drawGraph();


protected:
    void paintEvent(QPaintEvent *event);


private:
    QPen pen;
    QBrush brush;
    QPixmap pixmap;
    QVector<QPoint> points;

};

#endif // GRAPH_H

source:

//graph.cpp

#include "graph.h"

Graph::Graph(QWidget *parent)
    : QWidget(parent)
{
    points.resize(0);

    setBackgroundRole(QPalette::Base);
    setAutoFillBackground(true);
}

void Graph::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setPen(QPen(Qt::NoPen));
    painter.setBrush(QBrush(Qt::green, Qt::SolidPattern));
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.drawPolygon(points);
}

QSize Graph::minimumSizeHint() const
{
    return sizeHint();
}

QSize Graph::maximumSizeHint() const
{
    return sizeHint();
}

QSize Graph::sizeHint() const
{
    return QSize(500, 200);
}

void Graph::addPoint(int w, int h)
{
    points.append(QPoint(w*2, h*2));
}

void Graph::clearPoints()
{
    points.clear();
}

void Graph::drawGraph() {
    points.prepend(QPoint(0,0)); //The base points of the graph
    points.append(QPoint(500,0));
    update();
    points.clear();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:27:21+00:00Added an answer on May 26, 2026 at 3:27 pm

    In drawGraph(), the call to update() posts an event notifying the widget to paint itself. You then clear the points and the drawGraph() call exits. After that, the event loop will process the update event and trigger a call to the paintEvent() but by then, there are no points in the vector of points to paint.

    Don’t think of the paintEvent() as painting something permanent onto the widget once that will be displayed forever until you clear it and paint something else. The paintEvent() needs to be able to paint the widget from scratch whenever it needs to be redrawn. This is often due to a request from the system when the widget is moved, minimized and restored etc. This means your vector of points needs to remain until you no longer want a polygon to be displayed or the points are changed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't have much PHP experience and I want to know how to best
Don't have much to say, just can get into the event handler. XAML: <Grid>
I don't have any experience in updating a Rails app and when I google
Don't know if this is possible or not. But I'm trying to make it
Don't you hate it when you have class Foobar { public: Something& getSomething(int index)
Don't let below code scare you away . The question is really simple, only
I have just tried to save a simple *.rtf file with some websites and
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Don't know how to explain it better but i'm trying to get a response
Don't they both have to convert to machine code at some point to execute

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.