1

Ok so i have created my UI with the drag and drop part of Qt and in the Ui i created a "Tab Widget" inside another "Tab Widget". Now, inside the second Tab Widget i created a Frame

My Question is, how do i draw on top of this frame in Qt?

i tried this but no luck at all. Ive tried different ways on going about this, the program runs good but when i go and see if the frame was drawn on, i see nothing...please help me with this, with code example and all. Thanks in advance.

void 2ndMainWindow::paintEvent(QPaintEvent *e)
{
    QPainter paint(ui->rightTriangle_frame);
    paint.drawEllipse(10,10,100,100);
    paint.setPen(Qt::red);


    paint.end();
}

The way i finally did it for those others that would like to know

my header file included this under the private slot

    Ui::GeometryMainWindow *ui;

    QGraphicsScene *scene;
    QGraphicsEllipseItem *ellipse;
    QGraphicsRectItem *rectangle;

In my .cpp file i did this

    ui->setupUi(this);

    scene = new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);

    QBrush redBrush(Qt::red);
    QBrush blueBrush(Qt::blue);
    QPen blackpen(Qt::black);
    blackpen.setWidth(6);

    ellipse = scene->addEllipse(10,10,100,100,blackpen,redBrush);
user1919840
  • 325
  • 1
  • 3
  • 10

2 Answers2

2

If you are going to draw some customized shapes on QFrame, I suggest you to use a QGraphicsView instead of QFrame. Implementing the paintEvent of a widget may create performance issues. If you use a QGraphicsView, you can create a QGraphicsScene and add your items to that scene which is the general and accepted way of doing this kind of stuff.

Alternatively, implement an eventFilter as below,

bool 2ndMainWindow::eventFilter(QObject *o, QEvent *e)
{
    if (e->type() == QEvent::Paint) {
    paintEvent((QPaintEvent *)e);
}
    return QMainWindow::eventFilter(o, e);
}

In constructor of your mainwindow add,

installEventFilter(this);

By the way, you should set your pen before drawing your ellipse in paintEvent function.

fatma.ekici
  • 2,537
  • 3
  • 24
  • 27
  • 1
    I tried this but another way doing this instead, it worked exactly how i wanted thanks alot =) scene = new QGraphicsScene(this); ui->graphicsView->setScene(scene); QBrush redBrush(Qt::red); QBrush blueBrush(Qt::blue); QPen bluepen(Qt::black); bluepen.setWidth(6); ellipse = scene->addEllipse(10,10,100,100,bluepen,redBrush); and header was QGraphicsScene *scene; QGraphicsEllipseItem *ellipse; QGraphicsRectItem *rectangle; i didnt really do what you said but i did use hraphics view which helped ALOT, thanks – user1919840 Dec 21 '12 at 20:39
1

It seems that you are overriding the paintEvent() method of your main window and from there trying to paint on the frame widget. Look at your console output - you should get a message like

QPainter::begin: Widget painting can only begin as a result of a paintEvent
QPainter::setPen: Painter not active
QPainter::end: Painter not active, aborted

Instead, subclass your QFrame widget (in Qt designer, select "Promote to ..." to define the user defined subclass) and in this subclass, override paintEvent like this:

void MyFrame::paintEvent(QPaintEvent *e) {
    QPainter paint(this);
    paint.setPen(Qt::red);
    paint.drawEllipse(10,10,100,100);

    paint.end();
    QFrame::paintEvent(e);
}

Note that, in order to draw a red ellipse, you need to set the pen color before drawing the ellipse, of course. You should also call the paintEvent() method of the parent class in order to draw the frame border.

See https://github.com/afester/StackOverflow/tree/master/QtRepaint for an SSCCE.

Andreas Fester
  • 34,015
  • 7
  • 86
  • 113
  • i followed what you said word by word and i get this error now, error: C2143: syntax error : missing ';' before '*' and error: C4430: missing type specifier - int assumed. Note: C++ does not support default-int it says that because instead of being QWidget *Right_Triangle_Tab; (being declared as a QWidget like everything else) now it shows like this, rightTriangle_frame *rightTriangle_frame; I tried doing QFrame rightTriangle_frame *rightTriangle_frame; but it doesnt work....what should i do? – user1919840 Dec 21 '12 at 00:22
  • You should rename your class `rightTriangle_frame` (in Qt designer) to something like `RightTriangleFrame` and add a `RightTriangleFrame.cpp` and a `RightTriangleFrame.h` file to your project. In this file, implement the `class RightTriangleFrame : public QFrame`. See the `MyFrame.cpp` / `MyFrame.h` files from the link in my answer. – Andreas Fester Dec 21 '12 at 06:05
  • i already did all of that already, i fixed that error, i get no error now but i still cannot see the drawing on top of the frame =(, there has to be something im missing here...i just cant fiind out what it is – user1919840 Dec 21 '12 at 07:09
  • Can you shrink down your code to the minimum required to reproduce the issue and post the relevant parts in your question? (Probably its better to post a new question, in order to not pollute this one) – Andreas Fester Dec 21 '12 at 07:22