Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

qImage not showing inside custom promoted widget



  • Hi, I am trying to recreate the Qt Scribble example. I got it working by creating everything manually, now I am trying to use Qt Designer.

    I need a custom class that subclasses QWidget so that I can reimplement the paint and mouse click events for scribbling.
    I wrote the class and headers (paintarea.h, and paintarea.cpp). And then promoted a QWidget in QTDesigner into this class.

    Code is:
    paintarea.h

    #ifndef PAINTAREA_H
    #define PAINTAREA_H

    #include <QWidget>
    #include <QtWidgets>

    class PaintArea : public QWidget
    {
    Q_OBJECT
    public:
    explicit PaintArea(QWidget *parent = 0);

    private:
    QImage image;

    signals:

    public slots:
    };
    #endif // PAINTAREA_H

    paintarea.cpp

    PaintArea::PaintArea(QWidget *parent) : QWidget(parent)
    {
    image = QImage (500, 500, QImage::Format_ARGB32_Premultiplied);
    image.fill(qRgb(200,0,0));
    }

    Nothing displays inside the widget. I have tried for example a qSlider, and it shows up. But qImage won't work for some reason. I have tried isNull() to determine if the image is null, but it is not. It's just invisible.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    QImage is not a QWidget, if you want to see what it contains, you need to either use a QLabel to show it or paint it in a widget.



  • @SGaist But why does the QImage show if I code everything manually without using a promoted class in QTDesigner? Then the code above works.

    I'd like to use QTDesigner because it makes the UI work a lot faster but it seems to add a lot of headache as well.


  • Lifetime Qt Champion

    The exact same code shows a red image ?



  • @SGaist Yes, it's actually supposed to be a white background that you can paint on (like in the Qt Scribble example). And it works if I add all the elements manually, but in another project that uses QtDesigner with a QWidget promoted to PaintArea class, the QImage no longer shows up.


Log in to reply