PDA

View Full Version : how to show fillRect() using mouseEvents??



thomasjoy
31st August 2007, 06:17
hi all
i am using QT4.1 on Mac
i had drawn four column using QPainter class
and drawn some text in them
now i want to select the particular portion of text from that table just same as like QtextEdit ,in TextEditparticular selected portion get pink color,

how the same is possible using QPainter and QMouseEvents??
do tell me with siple code example if anyone know??

thanks in advance....

thomasjoy
31st August 2007, 10:52
hi all
i am using QT4.1 on Mac

i had created a table with data in diffrent columns using QPainter
now i am selecting data from table on mousePressEvent()...and selcted upto mouseMoveEvent()...and upto that particular data i am using fillRect()...

it is working when debugged but it is not shown on run time

by debugging results i had seen mouseMove Event() is called again and again...so i want to know why its happening...and why its not showing painted rectangle


if anyone know do tell me by simple example code

i had done like this

void MainForm::mousePressEvent ( QMouseEvent * e )
{
//m_DataPainter->fillRect(m_SelectedRect,Qt::white);
m_StartingPointX= 0;
m_StartingPointX = e->pos().x();
m_StartingPointY= 0;
m_StartingPointY = e->pos().y();

}


void MainForm::mouseMoveEvent ( QMouseEvent * e )
{

if (e->type() == QEvent::MouseMove)
{
m_EndingPointX=e->pos().x();
m_EndingPointY=e->pos().y();
int x2=m_EndingPointX - m_StartingPointX;
int y2=m_EndingPointY - m_StartingPointY;
m_DataPainter->fillRect(m_StartingPointX,m_StartingPointY,x2,y2,Q t::lightGray);
}


}


thanks in advance

jacek
31st August 2007, 11:33
You shouldn't draw anything on your widget outside the paintEvent().

As the docs say:
Warning: When the paintdevice is a widget, QPainter can only be used inside a paintEvent() function or in a function called by paintEvent(); that is unless the Qt::WA_PaintOutsidePaintEvent widget attribute is set. On Mac OS X and Windows, you can only paint in a paintEvent() function regardless of this attribute's setting.

PS. Please, don't start more than one thread on the same topic.

Wizard
31st August 2007, 11:36
You must use fillRect() in paintEvent(...) only. Do something like that:


//somewhere in class
bool DrawSelection;

void MainForm::paintEvent(QPaintEvent* e)
{
QPainter painter(this);
if(DrawSelection)
painter.fillRect(m_SelectedRect,Qt::white);
...
}

and in mouse press event:


void MainForm::mousePressEvent (QMouseEvent * e)
{
...
m_StartingPointX= 0;
...
//check sizes and set
DrawSelection = true;
}

thomasjoy
31st August 2007, 11:56
thanks for reply.....
but by doing this data get hide and color get drawn on the data

and when i scroll down ,the painted Rectangle remain on that particular position which i had selected rather it should not to be come on ...

and on scroll up it should be repainted how it can be done??

Wizard
31st August 2007, 12:06
You need to convert pixels in line numbers (selected) in mouse move/press handlers and draw rectangle in coordinates calculated from line numbers. To make selection transparent, use QColor(255, 255, 255, 128).

thomasjoy
31st August 2007, 12:58
no i dont want transparent i want to set background color
and when i scroll down that area remain with position...but i want to change that and even for scroll up it should be repainted on that area

jpn
31st August 2007, 13:14
May I ask why don't you use QTextEdit?

thomasjoy
31st August 2007, 14:01
because the requirement is not like this and on textEdit it becomes very slow when i am showing file size more thn 2Gb


basically i dont require textEdit

so tell me how it can be possible using QPainter

Wizard
31st August 2007, 14:30
It is very simple task. You need some kind of display pointer. It may be position in source file or somethink like that. And in paintEvent you must render all data starting from this pointer. On scrolling events adjust that pointer and call update(); It is algorithm in just two words.