0

Here is another newbie to Qt.

What I need to do is to have a scrollable Area in the center of MainWindow, which displays images, and allows user to paint on the image.

Since I cannot add a QPixmap directly to a scrollable Area, I tried to create a subclass of QWidget, like below:

class Canvas: public QWidget
{
public:
    Canvas(){
    image = new QPixmap(480,320);
    image->fill(Qt::red);
    }
    QPixmap *image;
};

Then I declared Canvas *c in the header file.

In the implementation, I wrote:

  canvas = new Canvas;
  setCentralWidget(canvas);

However, apparently this does not help to show up the QPixmap. I do not know what to do.

1 Answer 1

3

You don't need to subclass QWidget for this. QPixmap is not a widget, so it is not shown anywhere. You need to add your pixmap to some widget, this will work:

in header:

QLabel* imageLabel;

in cpp:

imageLabel = new QLabel(this);
QPixmap image(480,320);
image.fill(Qt::red);
imageLabel->setPixmap(image);
setCentralWidget(imageLabel);
3
  • The problem is that, if I do it this way, can I paint on the QLabel? By painting on the qlabel, does it directly changing the corresponding Pixmap? Mar 6, 2013 at 4:52
  • What if I want continuously changing the Pixmap? So everytime I have to call imageLabel->setPixmap() function? Mar 6, 2013 at 5:00
  • No, in that case I think it is better not to use QLabel. I mean, yes, you can call imageLabel->setPixmap() everytime, but it will affect performance a lot if you do that continuously. Take a look at QGraphicsScene, QImage, QGraphicsView... Mar 6, 2013 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.