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

  • Home
  • SEARCH
  • 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 8305089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:00:07+00:00 2026-06-08T18:00:07+00:00

I basically want to display a rectangle on a dialog window widget. Using another

  • 0

I basically want to display a rectangle on a dialog window widget. Using another question as reference, I tried to adapt the framework of using a QLabel and painting to it (the process overall seems overly complicated).

I started by making a member in the dialog box’s class:

QLabel* label; 

In the constructor of the dialog box:

label = new QLabel(this);
label->setGeometry(20, 50, 50, 100);

Just to try and make it work, I gave the dialog box a button to make the “rectangle” created with the label appear on the widget. I connected the “pressed” signal of this button to a slot which does the following:

QPixmap pixmap(50, 100);
pixmap.fill(QColor("transparent"));
QPainter painter(&pixmap);
painter.setBrush(QBrush(Qt::black));
painter.drawRect(20, 50, 50, 100);
label->setPixmap(pixmap);
update(); 

Unfortunately, nothing appears in the widget when I press the button. What am I missing here?

  • 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-06-08T18:00:09+00:00Added an answer on June 8, 2026 at 6:00 pm

    I tried this with PyQt and it generally works, but I’m not 100% sure about the procedure. Maybe you should try calling painter.end() the painter before calling setPixmap(). Also, I’m not sure if one is supposed to draw onto a QPixmap outside of QWidget:paintEvent, it might be safer to draw a QImage and create a QPixmap from it.

    from PyQt4 import QtGui
    
    app = QtGui.QApplication([])
    
    class Test(QtGui.QWidget):
        def __init__(self):
           QtGui.QWidget.__init__(self)
           self.bn = QtGui.QPushButton("Paint")
           self.lb = QtGui.QLabel()
           layout = QtGui.QVBoxLayout(self)
           layout.addWidget(self.bn)
           layout.addWidget(self.lb)
           self.bn.clicked.connect(self.handleClick)
    
        def handleClick(self):
           pixmap = QtGui.QPixmap(50, 100)
           pixmap.fill(QtGui.QColor("transparent"))
           p = QtGui.QPainter(pixmap)
           p.drawRect(0,0,50-1,100-1)
           p.end()       
           self.lb.setPixmap(pixmap)
    
    t = Test()
    t.show()
    app.exec_()
    

    For simply drawing a rectangle this is certainly very complicated. I don’t know what you are planning, be aware that there is QGraphicsView for drawing figures.

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

Sidebar

Related Questions

I basically want to open a browser window from Word using VBA that does
I want to display an image in a functional programming style. Basically, I operate
I have a financial calculator in MS excel, and I basically want to display
I am using 3 tables for this, Part, PartDrawing and ShopDrawing. Basically I want
I have a question related to jquery. Basically I want to do a very
What I basically want to do is tp display a sequence of images at
I want to display a dialog which contains HTML that is dynamically generated: $.ajax({
I basically want something like: TextView Button Button . . . TextView TextView SeekBar
I basically want to setup a relationship similar to Netflix's DVD rental model. There
I basically want to do: git checkout branchA git checkout -b branchB <commit_id> which

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.