[Qt-creator] Drawing on QWidget

Andre Poenitz andre.poenitz at mathematik.tu-chemnitz.de
Mon Jul 6 02:08:40 CEST 2009


On Mon, Jul 06, 2009 at 02:57:39AM +0400, Alex Williams wrote:
> > I understand that this touches a fundamental problem of epistemology
> > but most people I am aware of distinguish between "I cannot observe it"
> > and "it does not exist". Those people might also be tempted to take
> > http://doc.trolltech.com/4.5/designer-using-custom-widgets.html as an
> > indication that your proposition is wrong.
> 
> If something is not clear enough or hard to find = it's almost same as
> "it does not exists"... I did know that for such primitive action as
> drawing something - I nedd create widget...

The induction from "I could not find it" to "it is (generally) hard to find"
is lacking a proof. [I doubt there is one as I have a counter example:
I found that page within two minutes without ever seing it before]

>From a theoretical point we can stop here, but for the sake of completeness:
The definition of "is almost the same" is missing. I don't think there is
such a concept generally known in logic (but feel free to prove me wrong...)

And, as a really subjective point: Given your wxWidgets code below I am
fairly sure you knew that you had to create a widget for "such a
primitive action as drawing something". wxPanel is a widget, isn't it?

Anyway: What exactly did you look for in the documentation?

> > This seems to use definition of 'analog/competitor' that I would not
> > actively use myself, but out of curiosity: How would that work with
> > wxWidgets?
> 
> In wxWidgets everything is simple:
> 
> void Draw(wxPanel *DS)
> {
>   wxSize DC_Size;
>   DC_Size = DS->GetSize();
>   wxClientDC CDC(DS);
>   wxBufferedDC DC(&CDC, DC_Size);
>   wxBrush BackBrush(*wxWHITE);
>   DC.SetBackground(BackBrush);
>   DC.Clear();
> }

In Qt:

  void paintEvent(QPaintEvent *)
  {
    QPainter p(this);
    QPixmap pm("paint.png");
    p.drawPixmap(0, 0, pm.scaled(size()));
  }

or possibly:

  void paintEvent(QPaintEvent *)
  {
      QPainter p(this);
      QImage i = QImage("paint.png").scaled(size());
      p.drawPixmap(0, 0, QPixmap::fromImage(i));
  }

Obviously, simplicity lies in the eye of the beholder.

Andre'


PS: For reference the full "application":

  #include <QtGui>

  struct Widget : public QWidget
  {
    void paintEvent(QPaintEvent *)
    {
      QPainter p(this);
      p.drawPixmap(0, 0, QPixmap("paint.png").scaled(size()));
    }
  };

  int main(int argc, char *argv[])
  {
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
  }


PPS: If you don't need resizing, the following is shorter:

  #include <QtGui>

  int main(int argc, char *argv[])
  {
    QApplication a(argc, argv);
    QLabel l;
    l.setPixmap(QPixmap("paint.png"));
    l.show();
    return a.exec();
  }




More information about the Qt-creator-old mailing list