Results 1 to 5 of 5

Thread: Drawing shapes on a tabWidget in Qt

  1. #1
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Drawing shapes on a tabWidget in Qt

    Hi,

    I'm currently working on a very basic application that basically grebs the inputs form
    two lineEdits displays the result and creates rectanglular shapes when a button is clicked,the
    output is based on the two inputs, for instnce if the inputs were 2 and 3 it would draw a 2x3 rectangle (then I duplicate this rectangle based on other factors).

    This appliation is a MainWindow using a tabWidget that I created in Qt Creator, all of my buttons and lineEdits are sitting on top of the tabWidget, the problem I'm currently having is that when I draw my rectangles they are actually showing underneath the tabWidget, in other words everything is working except that the rectangular shapes are drawn underneath the tabWidget.

    Here is the code that caluculates and draws the recatangles
    Qt Code:
    1. void MainWindow::calculate()
    2. {
    3. // calculations here
    4. {
    5.  
    6.  
    7. //draw rectangles
    8. void MainWindow::paintEvent(QPaintEvent *event)
    9. {
    10. QPainter painter( this);
    11. QRect sheet (20, 20, 120,600);
    12. QBrush brush1(Qt::black, Qt::SolidPattern);
    13. painter.fillRect(sheet, brush1);
    14.  
    15. if(m_flag)
    16. {
    17. for (int i=0; i< 20; i++)
    18. {
    19. for(int j=0; j<20; j++)
    20. {
    21. QRect rec ((((partWidth+1)*i)+30), (((partLength+1)*j)+30), partWidth, partLength);
    22. QBrush brush(Qt::green, Qt::SolidPattern);
    23. painter.fillRect(rec, brush);
    24. }
    25.  
    26. }
    27. }
    28. }
    29.  
    30.  
    31.  
    32. // draw rectangles when on_pushButton_Calculate is clicked
    33. void MainWindow::on_pushButton_Calculate_clicked()
    34. {
    35. calculate();
    36. m_flag = true;
    37. update();
    38. }
    To copy to clipboard, switch view to plain text mode 

    Is there a way to draw something on a tabWidget?

    I know its hard to make a suggestion without actually seeing the whole code but I don't think somebody will want to see
    my poor code, I'm actully learning both Qt and C++ at the same time.

    Thanks a lot

  2. #2
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Drawing shapes on a tabWidget in Qt

    Hi,

    That happens because you are drawing on the canvas of MainWindow and you should draw in the canvas of the tab page. For this you would need to subclass the tab page and re-implement draw. However, If you want to draw shapes I would encourage you to use QGraphicsView + QGraphicsScene + QGraphicsRectItem.

    Carlos.

  3. #3
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Drawing shapes on a tabWidget in Qt

    Thank you for your reply.
    For this you would need to subclass the tab page and re-implement draw.
    How would you do this? Sorry about my newbie questions.

    However, If you want to draw shapes I would encourage you to use QGraphicsView + QGraphicsScene + QGraphicsRectItem.
    If I use the QGraphicsView do I need to subclass the tab page too?

    What is the difference between QPainter and QGraphicsView?

    Thanks a lot for your help!

  4. #4
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Drawing shapes on a tabWidget in Qt

    Hi,

    If I use the QGraphicsView do I need to subclass the tab page too?
    No you don't need to sublclass the tab page because you can place a QGraphicsView in that tab. I am assuming that you are using QTCreator or QT Designer to create your GUIs. Using either of both you can place a QGraphicsView in that page.

    What is the difference between QPainter and QGraphicsView?
    QGraphicsView is a widget that shows an drawing scene (sometimes called canvas). Each scene can have multiple graphical items (boxes, lines, polygons, etc). The QPainter class performs low-level painting on widgets and other paint devices. If you can to create you a widget from scratch (Analog clock example) then you need painter. But if you want to draw boxes, lines, polygons, etc use a QGraphicsView + QGraphicsScene.

    I recommend you to look at the examples: Diagram Scene also look at this thread: QGraphicsRectItem-with-list-of-QStrings

    Carlos

  5. #5
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Drawing shapes on a tabWidget in Qt

    Thanks a lot for your help. It looks like QPainter is basically to paint actual GUI components. I will try QGraphicsView, I think is what I need.

    Thanks a lot

Similar Threads

  1. Drawing and resizing shapes
    By jonnale in forum Qt Programming
    Replies: 10
    Last Post: 15th April 2020, 16:44
  2. computing intersections of two shapes
    By alpinista in forum Qt Programming
    Replies: 1
    Last Post: 20th August 2009, 11:23
  3. temporal shapes in drawing application
    By parnedo in forum Qt Programming
    Replies: 3
    Last Post: 29th July 2009, 13:07
  4. Drawing shapes
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 31st October 2008, 09:19
  5. Replies: 2
    Last Post: 9th March 2007, 02:28

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
  •  
Qt is a trademark of The Qt Company.