Re: Drawing over a button
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.
Re: Drawing over a button
Thanks for your answer.
months.h :
Code:
{
Q_OBJECT
int day;
public:
/
months(){}
void setmonths(months** box2,int month2);
void setmonths(int month2);
signals:
void rightClickedd();
protected:
public slots:
void whenrightclicked();
};
{
Q_OBJECT
int day;
public:
keyday(){}
};
months.cpp :
Code:
months* box[2];
{
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.
Re: Drawing over a button
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:
Code:
#include <QApplication>
#include <QPushButton>
#include <QPainter>
Q_OBJECT
public:
setText("Test");
}
protected:
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) {
CrossedButton b;
b.show();
return app.exec();
}
#include "main.moc"
Re: Drawing over a button
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.
Re: Drawing over a button
Quote:
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?
1 Attachment(s)
Re: Drawing over a button
Thanks for your answer.
Maybe this image can explain my question better:
Attachment 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.
Re: Drawing over a button
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.
Re: Drawing over a button
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.