PDA

View Full Version : not sure how to do this... please help



Slewman
6th October 2009, 15:16
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



VideoInterface::VideoInterface(QWidget *parent) :
QWidget(parent, Qt::FramelessWindowHint),
ui(new Ui::VideoInterface)
{
ui->setupUi(this);
}

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

scascio
6th October 2009, 16:03
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.

Slewman
6th October 2009, 16:08
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


Qpainter painter();
painter.drawLine(line);


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

Lykurg
6th October 2009, 16:09
See also the example "Basic Drawing Example" which comes with the documentation. See especially RenderArea Class Implementation.

Lykurg
6th October 2009, 16:10
Use
QPainter painter(this); inside the paint event.

Slewman
6th October 2009, 16:15
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

Lykurg
6th October 2009, 16:26
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.

Slewman
6th October 2009, 16:33
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?

Lykurg
6th October 2009, 16:45
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.

Slewman
6th October 2009, 16:50
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?

Slewman
6th October 2009, 16:56
void VideoInterface::paintEvent(QPaintEvent * /* event */)
{
QLineF line(10.0, 80.0, 90.0, 20.0);

QPainter painter(this);
painter.drawLine(line);
}

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

Slewman
6th October 2009, 17:01
ignore my last post, figure it out... but, still interested in relative coordinate positions if you have any insight

scascio
6th October 2009, 18:05
The QWidget::mapToParent or QWidget::mapFromParent and assimilate may be useful .