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 7540709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:42:19+00:00 2026-05-30T07:42:19+00:00

I have made my own simple widget by deriving from QWidget, and am trying

  • 0

I have made my own simple widget by deriving from QWidget, and am trying to get it to paint a gradient inside its paintEvent(). I set a breakpoint inside paintEvent, but it never gets called, even when I resize the window etc. The widget is a promoted QWidget from within the designer.
Here is my object code – I am not actually reading the gradient colors yet, just trying to get the paintEvent() to fire first hehe.
First, the header file:

#ifndef GRADIENT_H
#define GRADIENT_H

#include <QBrush>
#include <QPen>
#include <QPixmap>
#include <QWidget>


class Gradient : public QWidget
{
    Q_OBJECT

public:
    Gradient(QWidget *parent = 0);

    QSize minimumSizeHint() const;
    QSize sizeHint() const;

public slots:

protected:
    void paintEvent(QPaintEvent *event);
    void resizeEvent(QResizeEvent *event);

private:

};

#endif // GRADIENT_H

Then, the .cpp:

#include <QtGui>
#include <QPen>

#include "gradient.h"


Gradient::Gradient(QWidget *parent)
    : QWidget(parent)
{

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

QSize Gradient::minimumSizeHint() const
{
    return QSize(20, 256);
}

QSize Gradient::sizeHint() const
{
    return QSize(20, 512);
}

void Gradient::resizeEvent(QResizeEvent * /* event */)
{
    //update();
    repaint();
}

void Gradient::paintEvent(QPaintEvent * /* event */)
{
    QPainter painter(this);
    painter.fillRect(rect(), Qt::black);
    painter.setPen(QPen(Qt::blue,1));
   // painter.setBrush(?);

    for (int y = 0; y < height(); ++y)
    {
        painter.save();
      //painter.setPen(); <---here I plan to set different colors from a dynamic table
        painter.translate(0, y);

        painter.drawLine(0, 0, width(), 0);

        painter.restore();
    }
    painter.drawRect(QRect(0, 0, width() - 1, height() - 1));
}
  • 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-30T07:42:20+00:00Added an answer on May 30, 2026 at 7:42 am

    There is no need to call update() or repaint() inside the resizeEvent(). But that shouldn’t break anything either. There must be something wrong with the promoted stuff because that paintEvent() override is correct. I pasted it into a QWidget subclass on my end and it works. Perhaps a clean all and rebuild?

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

Sidebar

Related Questions

I have made my own file type (.ddd) and I made a simple program
I have made a simple experimental language of my own. I want make Eclipse
I made a simple WPF application in Visual Studio. It accesses its own database
I have made a class which a form can inherit from and it handles
I need your help. I have made a really clean and simple example to
I'm trying to disassemble a C/C++ DLL, and have made some progress, but I
The concept So, I've already made (upgraded actually) this website with its own Content
Assume I have simple protocol implemented over TCP, where each message is made up
I have made a SVG image, or more like mini application, for viewing graphs
I have made some code which exports some details of a journal article to

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.