PDA

View Full Version : Make different regions of QLabel act differently, like an image map



codeslicer
13th February 2008, 20:52
So this is the scenario:

I am making a custom QWidget dialog with an irregular shape. Since I have ommited the system's native window decorations, I need to reimplement that myself. At the top, I have a long image with different button "markers". I want the image to "change" depending on where the mouse is, and to also perform different actions depending on which part of the QLabel image was pressed. For example, if I have an image with 3 buttons drawn on it, I want each of those buttons to be brighter when it is hovered, and when one of the buttons is pressed, say the minimize button, the application would be minimized. Is this possible, say using subclassing QLabel, or is this out of reach of Qt? :mad:

Thanks in advance for any help, this must be possible somehow... Also, do I have to reimplement QLabel in its own class with the enterEvent() to use it, or can I use it inside my main widget's class?

So again, thanks in advance :) ~codeslicer

jacek
14th February 2008, 22:19
It should be possible. Take look at xxxEvent() methods of QWidget class (and don't forget about QWidget::mouseTracking property). You can also use event filters --- see QObject::installEventFilter().

codeslicer
15th February 2008, 23:53
So I have to make my own class? Or reimplement the QLabel class and add event handlers?

codeslicer
15th February 2008, 23:56
Well... I know this sounds nooby, but how would I use the installEventFilter()? I read the Key-press-eater, but how does that work? Do I have to make a seperate eventFilter class? And attach that to an object usign coding - not designer? THanks in advance - codeslicer

jpn
16th February 2008, 10:57
You can use any existing QObject subclass as an event filter. It could be for example your window class which loads the designed ui.

codeslicer
17th February 2008, 17:55
Ok... how would the code work for it though? And is there any way that I can integrate it into Qt Designer, or do I have to somehow place it in there using code only :confused:

jpn
17th February 2008, 18:44
Qt Designer is a layout designer. You don't implement functionality in Qt Designer. You implement functionality by writing code.

codeslicer
17th February 2008, 19:42
Ok... but how would I do that though? I know how to arrange the form, I'll figure it out by looking at the generate header-from-uic file, but how can I make that event filter and how would I use it with a QLabel (or a subclassed QLabel) :confused:

jpn
17th February 2008, 19:53
So how do you use that form you created in Qt Designer? Are you using single (http://doc.trolltech.com/4.3/designer-using-a-component.html#the-single-inheritance-approach) or multiple inheritance approach (http://doc.trolltech.com/4.3/designer-using-a-component.html#the-multiple-inheritance-approach)?

GTBuilder
17th February 2008, 20:13
I had a similar scenario with a row of PushButtons that I wanted to change color as the mouse was over them. They were implemented flat in QTDesigner with mouseTracking set true. Text in a QLabel also changed depending on the PushButton under the cursor.



void Clone::mouseMoveEvent( QMouseEvent *event)
{
int X = event->x();
int Y = event->y();

if( X > 0 && X < 160)
{
if( Y > 0 && Y < 80)
{
TWButton->setPalette( QColor( 255,255,255,255));
textLabel->setText( twString);
}
else
TWButton->setPalette( QColor( 0,170,255,255));

etc.


I'm sure you could accomplish the same thing with a group of QLabels merged together so they would look like a single object until changed by the cursor.

codeslicer
17th February 2008, 22:32
Finally, someone who answers not to just get their post count up :D. Brilliant idea, thanks a lot :)

codeslicer
17th February 2008, 22:36
Sorry jpn, didn't see your comment :(
I'm using multiple-inheritance, so do I have to have several QLabels in the same place, each being transparent in all places except for where the button would be? If so, how would I switch their order from top to bottom? :confused:


Oh, and in the code above, what would I replace Clone with, a subclass of a QLabel, or the name of my label that I want to switch?

Thanks in advance ~codeslicer

codeslicer
18th February 2008, 00:16
*bump* Anyone?

GTBuilder
18th February 2008, 00:20
Clone is the base widget that I populate with 4 QPushButtons, 2 QRadioButtons and 2 QLabels. The RadioButtons are invisible and superimposed on one of the pushbuttons. They are set visible after the button is clicked and set invisible again after one or the other is clicked launching a QProcess.

Intent is to emulate an html page.