Results 1 to 4 of 4

Thread: Constructing a custom widget in QGraphicsScene

  1. #1
    Join Date
    Oct 2019
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Constructing a custom widget in QGraphicsScene

    Hello!

    I am making an audio app in PyQt that will display custom graphs and I'm trying to use the QGraphicsScene. I plan to have the timeline on top and the graph just below, with the y-axis always visible on the left (despite horizontal scrolling). There won't be a vertical scrollbar. I thought using a vertical graphics layout might be just what I need.

    It seemed like QGraphicsWidget was the natural choice to subclass since QWidget is where custom widgets are made. However, as soon as I started, I couldn't find out how to paint the QGraphicsWidget. There's no paint event that I could find. I thought about creating a pixmap and adding it as a child, butI also couldn't find out how to add a child widget except upon creation of the child. This widget should scale, but it will need to change width as someone zooms in and out of the graph.

    Then I tried using a regular QWidget and add it to the scene, but the graphics layout didn't recognize it.

    I tried pyqtgraph, but there are too many changes I'm making and it's hard to find resources and help on it.

    So what is the best approach to create such a widget? If I use QGraphicsWidget, how do I paint it? I will also need to add all the numbers and lines on the timeline.

    Thanks!

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,165
    Thanks
    235
    Thanked 662 Times in 652 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Constructing a custom widget in QGraphicsScene

    QGraphicsWidget inherits from QGraphicsObject, which inherits from QGraphicsItem, which has a QGraphicsItem::paint() virtual method.

    However for your purposes you might find is easier to use QGraphicsProxyWidget to host a ready-to-go scientific graphics widget like Qwt or QCustomPlot instead of rolling your own.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Oct 2019
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Constructing a custom widget in QGraphicsScene

    Thank you for the response. So it's paint() instead of paintEvent().

    I'd be happy to try QCustomPlot and Qwt, but from looking at those links they are C++ projects whereas I'm working in PyQt5. That may be why pyqtgraph was built.

    Is there a way to use either of those projects in PyQt?

    Also, can you please tell me how to add child widgets to QGraphicsWidget?
    Thanks!

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,165
    Thanks
    235
    Thanked 662 Times in 652 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Constructing a custom widget in QGraphicsScene

    There is a PythonQwt project.

    Also, can you please tell me how to add child widgets to QGraphicsWidget?
    QGraphicsWidget is a base class, meant to be derived from to add custom painting and other features. It isn't of much direct use as far as I can see. If you want to embed an existing QWidget in a QGraphicsScene, then QGraphicsProxyWidget is what you want. Otherwise, you have to dip into C++ and write your own.

    If you don't need all of the signal / slot / event handling that QGraphicsWidget provides (by virtue of inheriting from QObject) and you want to write your own graphics, you should probably start by deriving from QGraphicsItem or one of its sub-classes.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QObject explicit constructing and QSharedData
    By kornicameister in forum Qt Programming
    Replies: 6
    Last Post: 12th March 2013, 23:32
  2. Replies: 0
    Last Post: 19th March 2012, 20:09
  3. Access to custom widget functions in a QGraphicsScene
    By meazza in forum Qt Programming
    Replies: 2
    Last Post: 15th April 2011, 10:21
  4. Constructing tray icon with QIcon
    By Tiansen in forum Qt Programming
    Replies: 7
    Last Post: 7th October 2009, 07:18
  5. Constructing QImage from QBytearray
    By dbrmik in forum Newbie
    Replies: 6
    Last Post: 16th December 2008, 16:00

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.