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.

Faery
  • 4,552
  • 10
  • 50
  • 92
user1819047
  • 667
  • 9
  • 18

1 Answers1

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);
Ilya Kobelevskiy
  • 5,245
  • 4
  • 24
  • 41
  • 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? – user1819047 Mar 06 '13 at 04:52
  • What if I want continuously changing the Pixmap? So everytime I have to call imageLabel->setPixmap() function? – user1819047 Mar 06 '13 at 05: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... – Ilya Kobelevskiy Mar 06 '13 at 16:17