Results 1 to 13 of 13

Thread: not sure how to do this... please help

  1. #1
    Join Date
    Sep 2009
    Posts
    64

    Default not sure how to do this... please help

    i am slightly new to qt and im not sure how to implement this graphic...

    i am doing most of my design in qt designer and then implementing things that cant be done in designer in visual studio. im also not the most proficient c++ programmer, but i can hold my own.

    with that in mind, i am trying to create a frame or widget (anything really that can achieve my goal) that i can split into 4 regions by drawing a line that divides the frame (or whatever) into 4 triangular regions. i want to be able to fill each region with either a green or red background.

    i have tried looking into QPainter but am having trouble even getting visual studio to even recognize the class (here is where my c++ skills are lacking)

    here is the first line of my constructor just so you can have an idea of how im referencing widgets from my designer *.ui file

    Qt Code:
    1. VideoInterface::VideoInterface(QWidget *parent) :
    2. QWidget(parent, Qt::FramelessWindowHint),
    3. ui(new Ui::VideoInterface)
    4. {
    5. ui->setupUi(this);
    6. }
    To copy to clipboard, switch view to plain text mode 

    i will attach an image that is a basic idea of what im trying to achieve... any help will be greatly appreciated.

    thanks for your time
    Attached Images Attached Images

  2. #2
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: not sure how to do this... please help

    You can draw, but draw only, easily the image you provided as attachment
    in any QWidget derived object in overloaded method paintEvent.

    Playing with QPainter, you can set pen, brush, and draw shapes and paths that will adapt themselves on widget size.

    But this is only a picture, with no interaction.

  3. #3
    Join Date
    Sep 2009
    Posts
    64

    Default Re: not sure how to do this... please help

    yes i know this is only a picture... the end goal is to have something that i can change the regions from red to green or vice versa... im trying to get a QPainter object but visual studio is complaining about how im creating the QPainter object.

    all im doing is
    Qt Code:
    1. Qpainter painter();
    2. painter.drawLine(line);
    To copy to clipboard, switch view to plain text mode 

    what am i doing wrong here? its complaining that painter is not a struct class or union.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: not sure how to do this... please help

    See also the example "Basic Drawing Example" which comes with the documentation. See especially RenderArea Class Implementation.

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: not sure how to do this... please help

    Use
    Qt Code:
    1. QPainter painter(this);
    To copy to clipboard, switch view to plain text mode 
    inside the paint event.

  6. #6
    Join Date
    Sep 2009
    Posts
    64

    Default Re: not sure how to do this... please help

    so this does have to be inside the paintEvent function? and also, im looking through the basic drawing example, and i notice there is no actual call to the function paintEvent. is this how its supposed to be?

    also, with that being the case, how can i dynamically change my lines? say my program will change the line color according to the number value of an int, how can the paintEvent know when to update my new lines? do i call the function paintEvent in this case or is paintEvent one of those qt functions that is constantly being update

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: not sure how to do this... please help

    Quote Originally Posted by Slewman View Post
    so this does have to be inside the paintEvent function?
    yes.
    and also, im looking through the basic drawing example, and i notice there is no actual call to the function paintEvent. is this how its supposed to be?
    It is because an update() triggers a paint event.

    also, with that being the case, how can i dynamically change my lines? say my program will change the line color according to the number value of an int, how can the paintEvent know when to update my new lines? do i call the function paintEvent in this case or is paintEvent one of those qt functions that is constantly being update
    Just call QWidget::update() whenever you what to redraw your widget.

  8. #8
    Join Date
    Sep 2009
    Posts
    64

    Default Re: not sure how to do this... please help

    thank you so much, you have been a big help

    i just have one more question... if im trying to draw on a frame within my window, is it possible to make the frame a parent of the paint object? im trying to give it coordinates to draw the lines and my frame has the possibility of being moved in the window and it would be helpful if i can give it coordinates relative to this frame instead of having to calculate according to its position in the main window.

    is this possible?

  9. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: not sure how to do this... please help

    I am not sure if I get you right, but the coordinates you use for painting are always relative to the widget you are painting in. How you lay out that widget has nothing to do with the painting.

    Where do you want to use that "drawing widget"? maybe a QGraphicsView -> QGraphicsScene -> QGraphicsItem could also help you.

  10. #10
    Join Date
    Sep 2009
    Posts
    64

    Default Re: not sure how to do this... please help

    ill try to describe my application for you...

    its a main window with several buttons and display information. one of the buttons hides/shows a frame that has a couple more options... just like an options window, which is a QFrame with some more buttons. i want to draw on this options frame, with coordinates relative to the frame and not the main window.

    does this help?

  11. #11
    Join Date
    Sep 2009
    Posts
    64

    Default Re: not sure how to do this... please help

    Qt Code:
    1. void VideoInterface::paintEvent(QPaintEvent * /* event */)
    2. {
    3. QLineF line(10.0, 80.0, 90.0, 20.0);
    4.  
    5. QPainter painter(this);
    6. painter.drawLine(line);
    7. }
    To copy to clipboard, switch view to plain text mode 

    here are the errors i get

    c:\Development\twvs\PresQT\videoInterface.cpp(62): error C2228: left of '.drawLine' must have class/struct/union type
    type is 'int'
    c:\Development\twvs\PresQT\videoInterface.cpp(61): error C2079: 'painter' uses undefined class 'QPainter'

    i am programming in visual studio and for some reason it doesnt like the QPainter class

  12. #12
    Join Date
    Sep 2009
    Posts
    64

    Default Re: not sure how to do this... please help

    ignore my last post, figure it out... but, still interested in relative coordinate positions if you have any insight

  13. #13
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: not sure how to do this... please help

    The QWidget::mapToParent or QWidget::mapFromParent and assimilate may be useful .

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.