You are not logged in.

konopiux

Beginner

  • "konopiux" is male
  • "konopiux" started this thread

Posts: 12

Location: Lithuania

  • Send private message

1

Saturday, December 10th 2011, 12:23pm

painting in QWidget

Hi,
can anyone suggest any issue,
i want to paint in QWidget with QPaintEvent containing method
but i don't know how to call it in right way, to make it work.
Due to that i use netbeans and my form i create with QTDesigner i placed my method
in form class, but it doesn't work.
there is my code
Field.h

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef _FIELD_H
#define	_FIELD_H

#include "ui_Field.h"
class Field : public QDialog {
	Q_OBJECT
public:
	Field();
	virtual ~Field();
private:
	int currentLevel;
	Ui::Field widget;
protected:
	void Paint(QPaintEvent* event);
};

#endif	

Field.cpp

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "Field.h"

Field::Field() {   
	widget.setupUi(this);
	widget.pole->setPalette(QPalette(QColor(170, 255, 0)));
	widget.pole->setAutoFillBackground(true);
	currentLevel=45;
	update();

}
Field::~Field() {}
void Field::Paint(QPaintEvent* )
{
	QPainter painter(widget.pole); //widget.pole is that QWidget object where i want to draw
	painter.drawText(200,200,tr("Level= ")
        	+QString::number(currentLevel));
}

it would be nice if someone could help me with this.


P.S. sorry for my bad english
:pinch:

2

Saturday, December 10th 2011, 12:28pm

you need to call your method

virtual void paintEvent ( QPaintEvent * event );

see here:
http://doc.qt.nokia.com/latest/qwidget.html#paintEvent

I think you will have problems trying to paint on otherwidgets though. If you waint to paint on widget.pole, then paintEvent(...) should be on widget.pole's class, not Field.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

konopiux

Beginner

  • "konopiux" is male
  • "konopiux" started this thread

Posts: 12

Location: Lithuania

  • Send private message

3

Saturday, December 10th 2011, 12:38pm

just, using QTdesignet, it doesnt create any seperate class for object/widget, all information is contained in files that i cant see or edit
so how to put the paint event to the widgets.pole class?

4

Saturday, December 10th 2011, 12:59pm

How do I know what widgets.pole is?

You mean QtDesigner? That just makes a ui file and some code to make the widgets. Use that code to make your own class.


whatever class 'pole' is, you need to sub-class it, and implement paintEvent(...)
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

This post has been edited 1 times, last edit by "Amleto" (Dec 10th 2011, 1:07pm)


konopiux

Beginner

  • "konopiux" is male
  • "konopiux" started this thread

Posts: 12

Location: Lithuania

  • Send private message

5

Saturday, December 10th 2011, 1:04pm

widget.pole it is QWidget
yes QtDesigner, can you show example how to make my own class using that code

6

Saturday, December 10th 2011, 1:08pm

class MyWidget : public QWidget
{
protected:
void paintEvent(...);
};
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

konopiux

Beginner

  • "konopiux" is male
  • "konopiux" started this thread

Posts: 12

Location: Lithuania

  • Send private message

7

Saturday, December 10th 2011, 1:14pm

thnx,posibly the last queston:), but how to add it to the form created with designet, or i have now to create all other
objects on form by my own?

8

Saturday, December 10th 2011, 1:31pm

unless you can manage to figure out how to put custom widgets in QtDesigner (I think it is possible), then you need to create all manually now.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

konopiux

Beginner

  • "konopiux" is male
  • "konopiux" started this thread

Posts: 12

Location: Lithuania

  • Send private message

9

Saturday, December 10th 2011, 1:51pm

i see..
thnx for helping

Similar threads