views:

184

answers:

1

I've figured out how to use QPainter to draw rectangles. Now I want to have a drawing area where if the user clicks, a 1x1 rectangle is drawn where the mouse pointer is. To accomplish this, I assume I need a transparent Qt widget that supports the clicked() signal.

How do I make such a transparent widget? Or is there something else I can use? Perhaps I can only use the window's clicked() signal?

A: 

You don't really need a transparent widget?

All you have to do is implement

protected:
  void mousePressEvent(QMouseEvent *event);

for your widget and draw your rectangle.

Take a look at scribble example http://doc.qt.nokia.com/4.6//widgets-scribble.html that comes with Qt. It should help.

Anton
Looks helpful, but this tutorial does way more than I need so it's somewhat confusing. I added two files called `drawingPad.cpp` and `drawingPad.h` to the project that contain the class definition of DrawingPad. I added this to the MainWindow constructor: `drawingPad = new DrawingPad; setCentralWidget(drawingPad);` Then I get this error: "drawingPad was not declared in this scope". What are possible causes? I did include drawingPad.h. Does Qt Designer have a special feature to import custom widgets?
Pieter
http://doc.qt.nokia.com/4.6/designer-creating-custom-widgets.htmlI can only guess but maybe you need `DrawingPad *drawingPad = new DrawingPad;`
Anton
Now I get the "collect2: ld returned 1 exit status" error.
Pieter
@Pieter there has to be something before, maybe `undefined reference to` or something similar. Probably you forgot to link against qt or forgot to implement a method ...
Polybos
Nope, just that one error. http://img411.imageshack.us/img411/8043/errorbf.jpg
Pieter
Any declared, but not implemented functions - DrawingPad's destructor for example?
Anton
I found out I had declared but not implemented the destructor. I took the destructor declaration out of DrawingPad.h but I'm still getting the error. I'll zip up all the code tomorrow so I can post it here.
Pieter
Alright, here's my code so far... http://www.box.net/shared/jvvsb6hhto (Disclaimer: I'm very new to C++ and Qt.)
Pieter
@Pieter: You forgot to include mainwindow.ui file. In main.cpp you don't need to #include DrawingPad.h and also use the same case for files and include eg - drawingPad.h.
Anton