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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:30:02+00:00 2026-05-28T16:30:02+00:00

I have just wrote a little example and i can’t manage to make it

  • 0

I have just wrote a little example and i can’t manage to make it run.

from PyQt4 import QtGui, QtCore
import sys

class Drawer(QtGui.QWidget):

    def __init__(self, parent=None):
        super(Drawer, self).__init__(parent)
        self.setStyleSheet("QWidget { background-color: %s }" % QtGui.QColor(99, 0, 0).name())

    def mousePressEvent(self, event):
        print 'mouse pressed'
        self.update();

class MyApp(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        self.drawer = Drawer(self)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    myapp = MyApp()
    myapp.show()
    sys.exit(app.exec_())     

widget is not shown (no color, the window is gray) and if i press the mouse no print..

where is my error?

SOLVED: as qiao just point me in a comment, my error is the way to add a widget in the qt4 kind of scenegraph. i thought i had to call the parent in init and that’s all. This is not enogh, i have to add a QLayout and add the childs in it (this is quite obvious: the method addWidget is written only in QLayout and not in QWidget and having a scenegraph system without the possibility of adding new child is quite weird)

  • 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-28T16:30:04+00:00Added an answer on May 28, 2026 at 4:30 pm

    You have to set the central widget of the main window to be the drawer. Otherwise the drawer will not attached to the main window.

    class MyApp(QtGui.QMainWindow):
        def __init__(self, parent=None):
            QtGui.QWidget.__init__(self, parent)
    
            self.drawer = Drawer(self)
            self.setCentralWidget(self.drawer)
    

    After the above fix, you will see mouse press event working properly.

    As for the color, setting the stylesheet of QMainWindow is fine, so is setting Drawer to be another widget(like QLineEdit). I don’t know what’s the matter here.

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

Sidebar

Related Questions

I have to write my own hash function. If I wanted to just make
I have a macro to write, quite simple. It just pulls numeric values from
I have just installed composer in my /usr/bin folder, so when from that folder
Just wondering if anyone can help me with a little programming math i'm having
I wrote few custom view helpers but I have a little trouble using them.
Just trying to write my first ever WP7 app. I have several hundreds of
I have just created this table: create table dbo.OrderTypes ( ID smallint primary key
we have just started using a git account of our Django website project so
I have just started using RSpec and I copied the very simple test on
I have just started implementing ISet 's instead of IList 's in my project

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.