PDA

View Full Version : Drawing over a button



Awareness
13th October 2013, 05:36
I have a custom button class derived from QPushButton.I want to draw a "/" over this button and some parts of "/" will be outside of this button.When I click on the button,it will emit a right click signal(actually I did signal-slot code and it works correctly).

I tried creating another custom button class and using qpainter with paintevent.But seems like actually it draws another button over my button,so I can't right click on the button under it.

How can I make this work?

ChrisW67
13th October 2013, 09:29
How can we tell you how to make your code work... We only have the vaguest idea what you have actually done. Perhaps you could show us some code.

Awareness
14th October 2013, 01:20
Thanks for your answer.


months.h :



class months:public QPushButton
{

Q_OBJECT
int day;


public:

/

months(){}
months(QWidget* frame ):QPushButton(frame){}


void setmonths(months** box2,int month2);

void setmonths(int month2);

signals:
void rightClickedd();

protected:

void mouseReleaseEvent(QMouseEvent *e );

public slots:
void whenrightclicked();

};



class keyday:public QPushButton
{

Q_OBJECT
int day;


public:



keyday(){}
keyday(QWidget* frame ):QPushButton(frame){}


void paintEvent(QPaintEvent*);


};


months.cpp :


months* box[2];

void keyday::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setPen(Qt::white);
painter.setFont(QFont("Arial", 15));
painter.drawText(rect(), Qt::AlignCenter, "/");

}



void months::setmonths(int month2)
{
keyday* button1 =new keyday(frame2);
box[1]= new months(frame2);

box[1]->setGeometry(47,2,70,15)
button1->setGeometry(47,2,70,25);
button1->show();
}


I am trying to paint "/" over box[1] and I tried using QPainter,but seems like it needs to be painter over a widget.So I created a new class derived from QPushButton(also tried QLabel).Now it paints "/" over box[1] but I can't click on box[1] because when I click on box[1] it actually clicks on button1.I tried QPainter without using it inside a derived class' QPaintEvent,but it doesn't work in that way I think.

There is wxMemoryDc class in Wxwidgets for doing what I want ,I am looking for a similar thing in Qt.

ChrisW67
14th October 2013, 06:07
I cannot fathom the logic of your setmonths() function. Nevertheless, drawing a line on top of a button without breaking its function is straightforward enough:


#include <QApplication>
#include <QPushButton>
#include <QPainter>

class CrossedButton: public QPushButton {
Q_OBJECT
public:
explicit CrossedButton(QWidget *p = 0): QPushButton(p) {
setText("Test");
}

protected:
void paintEvent(QPaintEvent *event) {
QPushButton::paintEvent(event);

QPainter painter(this);
painter.setPen(Qt::red);
painter.setFont(QFont("Arial", 15));
painter.drawText(rect(), Qt::AlignCenter, "/");
// OR
// painter.setRenderHint(QPainter::Antialiasing, true);
// painter.setPen(QPen(Qt::red, 2));
// painter.drawLine(rect().topLeft(), rect().bottomRight());
}
};

int main(int argc, char **argv) {
QApplication app(argc, argv);
CrossedButton b;
b.show();
return app.exec();
}
#include "main.moc"

Awareness
15th October 2013, 03:46
Thanks for your answer.

Is it possible to use QPainter for drawing without using it inside a widget?If I use it inside a widget and put that widget on another widget,when I run the program,I can't click on the lower widget.

ChrisW67
15th October 2013, 07:39
If I use it inside a widget and put that widget on another widget,when I run the program,I can't click on the lower widget.
Qt does the sane thing: the top-most widget under the mouse pointer is the one that gets first look at the click event. The widget can chose to ignore the event or, as makes sense for a button, consume it and be "pushed".

I have given you a button with an overdrawn line that you can click regardless of the line.

It sounds like you have a poorly designed UI.
Exactly what effect are you trying to achieve with this line?

Awareness
16th October 2013, 02:51
Thanks for your answer.

Maybe this image can explain my question better:


9698


How can I do this using QPainter?Please click on the image (to make it larger), in small version the red line's borders are not visible clearly.

stampede
16th October 2013, 10:45
One way i can think of is to have the button embedded in QGraphicsScene as QGraphicsProxyWidget (via QGraphicsScene::addWidget). Then you can draw whatever you want on it with QPainter.

Awareness
18th October 2013, 04:30
Actually,after I sent my previous message with picture,I thought it was not a good idea to do it like I do it in the picture(at least for my current little project) and I used ChrisW67 's code in his reply,it is working good.But I didn't take my last question back,because I thought I may need the answer in the future and also I wondered if it was possible to do it in Qt.Thank you both ChrisW67 and stampede for your answers.