PDA

View Full Version : pls help. Need Qpainter to draw over a background image of mainwindow



athulms
12th September 2011, 11:55
6839

I am developing a qt wordsearch game.
I have to draw lines on the image when i finds a word from the words panel shown below the letter grid.
When i draw the line its drawn under the image so i can't see the line on the screen.
Hope u understood my question. Pls help. Thanks in advance

high_flyer
12th September 2011, 15:09
When i draw the line its drawn under the image so i can't see the line on the screen.
That is because you are probably not drawing on the correct widget.
Draw on the widget which contains your image.

athulms
12th September 2011, 16:02
i have set the image as style sheet of a frame(ui).
How can i use painter on the frame created in ui.
Pls help

stampede
12th September 2011, 16:36
How can i use painter on the frame created in ui.
Create a subclass of QFrame (or QWidget) and reimplement paintEvent. Use "promote to..." feature to place it on the ui form (right click on the widget in designer -> "promote to.." and type the details in popup ).

athulms
13th September 2011, 07:40
can you pls provide me some code??

I have created a new class
#include <QtGui>
#include <QDebug>

class Frame : public QFrame{
Q_OBJECT
public:
Frame( QWidget * parent = NULL ) : QFrame(parent){
setStyleSheet( "Frame{ background-image: url(plasma.png); }" );
}
protected:
void paintEvent( QPaintEvent * event ){
QFrame::paintEvent(event);
QPainter p(this);
p.drawLine( rect().topLeft(), rect().bottomRight() );
p.drawText( rect().center(), "works!" );
}
};

I have drawn the line in the subclass frame. Now how can i promote the frame over my mainwindow(ui)

stampede
13th September 2011, 08:08
You already have all needed code, now you have to use the designer:
- place the QFrame on the main widget
- right click on that frame, select "Promote to..."
- type in the class name and header file
- (!) save the modified form
- run qmake and compile the project again

athulms
13th September 2011, 09:55
k it worked for me. But when i am accessing the points from mouse click of button. it have to draw the lines. It draws lines when i give static points. when i am giving dyanamic points the prprogram runs but shows non-image error

stampede
13th September 2011, 10:21
But when i am accessing the points from mouse click of button. it have to draw the lines. It draws lines when i give static points. when i am giving dyanamic points the prprogram runs but shows non-image error
What can we do about that without seeing a single line of code, or the "non-image error" ?

athulms
13th September 2011, 11:19
void MainWindow:: drawpoint()
{
{calculations of mouse click point, The mouse click point is store in a Qpoint point}
update();
}

void drawing:: paintEvent(QPaintEvent *painter)
{
MainWindow *main=new MainWindow;
QFrame::paintEvent(painter);
QPainter p(this);
QPen linepen(Qt::cyan);
p.setRenderHint(QPainter::Antialiasing,true);
p.setRenderHint(QPainter::SmoothPixmapTransform,tr ue);
linepen.setWidth(25);
linepen.setCapStyle(Qt::RoundCap);
p.setPen(linepen);
p.setOpacity(0.7);
p.drawPoint(main->point);
}



I have used the above code.
I have arranged buttons in 11 X 11 format in a grid. I connected all the buttons to the drawpoint function. My drawing function paintevent is of drawing class.
when i call the update function it does not invoke the paintevent of drawing class.


I tryed to create an object like



drawing *draw=new drawing


then i sent the point by calling
draw->update
but the qt shows an error "no appropriate default constructor available"

wysota
13th September 2011, 11:51
Why are you creating a new window every time paintEvent() of another widget is called? What is "drawing" and how are you updating it?

By the way, Qt doesn't show you any errors, the compiler does -- your problem is strictly of C++ nature.

athulms
14th September 2011, 10:08
k i have completed my drawing. Thanks for ur help wysota, stampede