You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

Marrt

Unregistered

1

Wednesday, July 28th 2004, 1:46pm

QPainter and recursion

hi,
i'm using a QPainter to paint on a window. work (not fine, but works) when used with an iterative algorithm inside the function. but used with recursive functiions it seems not to show my points at all.

this it is:

function paint();
{...
QPainter painter(this);
..
painter.setPen(QColor(red));
painter.drawPoint( 200, 200);
...}

this one does the job. the next one does not:

function paint2();
{ ...
QPainter painter(this);
..
recpattern(200, 200, 0);
...}
with:
paint_class::recpattern(int x, int y, int level)
{
if (lev el==0)
{
painter.setPen(QColor(red));
painter2.drawPoint( x, y);
}
}

if this is not possible, is there another tool instead of Qpainter that works more consistent? because, when i move something over the painted area the painting is gone ..

thx alot for your help

Marrt

Marrt

Unregistered

2

Wednesday, July 28th 2004, 1:52pm

if you know better ways for paint-events - i only need drawPoint or an equivalent and setColor ..

i have another problem. the window which shows the paint-event is of QDialog and it closes immediatly after the paint show is done. how does it stay open?

thx again,
Marrt

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

3

Wednesday, July 28th 2004, 5:03pm

The problem probably is because you don't pass the painter object as an argument to your recpattern. Change it to paint_class::recpattern(QPainter &painter, int x, int y, int level); and call it as recpattern(painter, 200, 200, 0); and it should be ok.

Marrt

Unregistered

4

Wednesday, July 28th 2004, 5:55pm

Thx, now everything is fine.
Err .. there is still my problem with the closing windows. Any suggestions how to let them stay?

Marrt

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

5

Wednesday, July 28th 2004, 10:33pm

Could you paste here the code you have to open them? You probably put the window on stack and some scope ends and thus all the resources on stack are freed (along with your window) - it is probably the case, but provide the code to be sure.

Marrt

Unregistered

6

Thursday, July 29th 2004, 6:52am

the window is generated through a connection (signal is pushbutton.clicked()) of another window wich is set as mainWidget.. the first one (my problem child) is of QDialog, the other of QMainWindow.
when i generate it (the dialog) in main.cpp it stays on screen. otherwise not.

i use the exec() to show the window, but show() does the same (bad ;)) job.
setActiveWindow does not help either ..

actually, there is another question: how to make a qColorDialog appear?

thxs again,
Marrt

rhg

Intermediate

  • "rhg" is male

Posts: 179

Location: Velbert, Germany

Occupation: senior software developer

  • Send private message

7

Thursday, July 29th 2004, 8:30am

So in your slot where your Dialog is created, you probably create the Dialog as auto-variable on the stack, such as:

Source code

1
MyDialog dlg(this);
This causes the dlg object to be destructed as soon as the slot function finished (i.e. as soon as the dlg variable goes out of scope).

If you instead do the following:

Source code

1
MyDialog *dlg = new MyDialog(this);
the dialog will stay open because you created it on the heap, not on the stack.

This is actually basic C++ behavior, it is nothing Qt specific ;)

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

8

Thursday, July 29th 2004, 10:11am

Quoted

Originally posted by Marrt
actually, there is another question: how to make a qColorDialog appear?


Like this:

Source code

1
QColor color = QColorDialog::getColor();


You can add parameters to the function to change its behaviour. Also remember to #include <qcolordialog.h>.

This post has been edited 1 times, last edit by "wysota" (Jul 29th 2004, 10:13am)


Marrt

Unregistered

9

Thursday, July 29th 2004, 10:31am

ok i did it this way .. but isn't the solution.
but maybe now something like setActiveWindow works? no, also not.

but thx for the help,
Marrt

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

10

Thursday, July 29th 2004, 10:36am

But what is wrong? A window isn't on top of the window stack or what? What exactly is the problem here?

Marrt

Unregistered

11

Thursday, July 29th 2004, 10:42am

the Qcolor works fine. thx.

and now here is the (hopefully) last question:
how can i map a Qcolor to int and back? that would be very very helpful.

Marrt

Marrt

Unregistered

12

Thursday, July 29th 2004, 10:45am

the problem is:
-i generarte the window through a slot-connection
-the window appears
-the window generates its visualization
-the window disappears / closes

.. but i want to have a look at my visualisation ;)


Marrt

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

13

Thursday, July 29th 2004, 10:49am

Quoted

Originally posted by Marrt
how can i map a Qcolor to int and back? that would be very very helpful.


What do you mean by that? QColor consists of three composites - red, green and blue, so you can only map a QColor to three ints using the functions red(), green(), blue() to map to ints and setRgb(r,g,b) to map from ints.

About your vanishing window... can you paste (either as code or as an attachment) here the EXACT code you have for creating and showing the window?

Marrt

Unregistered

14

Thursday, July 29th 2004, 11:13am

VisualisationMain::create()
{...
if (RadioButton2->isChecked())
{

darstellung_LbyL windowLbyL = new darstellung_LbyL(this);
windowLbyL.create(dataDialog);
}
...}

darstellung_LbyL::create(Datendialog &dataDialog)
{
// some labels
...
exec(); //show();
...
// visualization part
...
}

that's the code.
VisualisationMain::create() is a slot-function

what's about Qrgb? this seems to be a pixel value. no chance to "int" it?
but then, how do i define a function set returns a QColor?

qcolor class::function(int Something)
{
//how do i specify the r-g-b-parts here?
}

god, i hope i'm not too annoying, but you seem to be a neverending fountain of help :D

Marrt

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

15

Thursday, July 29th 2004, 12:34pm

Hmm... and it compiles? Strange...

Source code

1
2
 darstellung_LbyL windowLbyL = new darstellung_LbyL(this);
windowLbyL.create(dataDialog);


It should be:

Source code

1
2
 darstellung_LbyL *windowLbyL = new darstellung_LbyL(this);
windowLbyL->create(dataDialog);


This way you create the window on the heap and not on the stack (object created on the stack will be destroyed when the function ends).

Source code

1
2
3
4
5
6
7
8
9
darstellung_LbyL::create(Datendialog &dataDialog)
{
// some labels
...
exec(); //show();
...
// visualization part
...
}


It'd be better if you did:

Source code

1
2
3
4
5
6
7
8
9
darstellung_LbyL::create(Datendialog &dataDialog)
{
// some labels
...

// visualization part
...
exec(); //show();
}


If you exec(), you will receive a modal dialog (one that blocks all other windows) and if you show(), you will get a non-modal dialog (you can still use other windows). It's better to put the call after all visualization, because when the window pops up, it will have all the content already rendered and a situation that the window pops up and you see a process of drawing the content (if the system is under heavy load).

Quoted

what's about Qrgb? this seems to be a pixel value. no chance to "int" it?
but then, how do i define a function set returns a QColor?


QRGB is an unsigned int (it has to be unsigned, because it consists of three positive values)

To make a QColor out of an uint, do:

Source code

1
2
QRGB myint;
QColor color(myint);

Marrt

Unregistered

16

Thursday, July 29th 2004, 1:14pm

[quote][i]Originally posted by wysota[/i]
Hmm... and it compiles? Strange...[/quote]

maybe because it's qt 2.3? i forgot to tell the version

[code] darstellung_LbyL *windowLbyL = new darstellung_LbyL(this);
windowLbyL->create(dataDialog);[/code]

this one does the job. thanks.

[quote]
It'd be better if you did:

[code]darstellung_LbyL::create(Datendialog &dataDialog)
{
// visualization part
...
exec(); //show();
}[/code][/quote]

but in that case (i tried it), the window is shown and all the labels, but NOT my painter-visualization.

[quote]
QRGB is an unsigned int (it has to be unsigned, because it consists of three positive values)

To make a QColor out of an uint, do:
[code]QRGB myint;
QColor color(myint);[/code][/quote]

ok thanks. if i have the rgb-values of a color, how do i translate them into the corresponding qrgb? 255255255 doesnt work correct, i think.

Marrt

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

17

Thursday, July 29th 2004, 1:33pm

Quoted


but in that case (i tried it), the window is shown and all the labels, but NOT my painter-visualization.


If you have a QPainter and visualize using it, it has to be on the visible widget (my mistake, I didn't specify that).

Quoted


ok thanks. if i have the rgb-values of a color, how do i translate them into the corresponding qrgb? 255255255 doesnt work correct, i think.


Remember that computers don't use decimal values, but hexadecimal... in your case, instead of 255255255 you should use 0xFFFFFF which is 16777215 decimal (white colour). 255255255d is 0xF36E2D7 which is slightly too big (it has more than 24 bits and colours are 24bit wide).

Marrt

Unregistered

18

Thursday, July 29th 2004, 1:58pm

so the first two digits stand for the r-value (00-FF), the next two for the g and the last two for b?

so i only have to:
convert color-parts decimal->hex?
concat them
convert whole thing in decimal
save in an array
read out
convert into hex
read the rgb-values out
convert them to dec?

Marrt

rhg

Intermediate

  • "rhg" is male

Posts: 179

Location: Velbert, Germany

Occupation: senior software developer

  • Send private message

19

Thursday, July 29th 2004, 2:04pm

Assumed you have three integers red, green and blue:

Source code

1
unsigned int rgb = (unsigned char)red | ((unsigned char)green << 8) | ((unsigned char)blue << 16);

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

20

Thursday, July 29th 2004, 2:12pm

Quoted

Originally posted by Marrt
so the first two digits stand for the r-value (00-FF), the next two for the g and the last two for b?


Yes, that's exactly it.

Quoted


so i only have to:
convert color-parts decimal->hex?
concat them
convert whole thing in decimal
save in an array
read out
convert into hex
read the rgb-values out
convert them to dec?


It depends on what do you want to achieve...

If you have three numbers (255, 255, 0) and want to make a colour out of them, you can simply do:

Source code

1
QColor col(255, 255, 0);

which is equal to

Source code

1
2
3
4
5
QColor col(0xff, 0xff, 0x0;[/quote]

or to change an existing colour:
[code]QColor col;
col.setRgb(255,255,0);


If you have a qColor and want to have three decimal numbers, you can use:

Source code

1
2
3
4
5
uint r, g, b;
QColor col;
r = col.red();
g = col.green();
b = col.blue();


You can of course do

Source code

1
QRgb rgb = col.rgb();
and then extract the values from this integer, but it is easier to do it with the previous method.

If you have an uint with the color data and want to make a QColor out of it, you do:

Source code

1
2
QRgb rgb = 0xffff00; // yellow
QColor col(rgb); // make col yellow