PDA

View Full Version : Drawing line on table



kocakden
27th April 2008, 01:49
Hi folks,
i just need to draw a line between one cell to an another(they do not have to be in the same row) on a table. I'm using Qt 4.3.4 for windows. I created the table and the cells. I thought that if i can get the coordinate of the cells i can draw the line but i couldn't find the function for it. Can anyone help me?

wysota
27th April 2008, 02:26
QAbstractItemView::visualRect()

kocakden
27th April 2008, 02:36
Thanks for quick reply. I read what you sent but i couldn't find out what to do. Can you explain a little bit more?

wysota
27th April 2008, 12:12
The method returns coordinates of an item you give it as its argument. I think that's what you wanted, right? Then you could do something like:

QModelIndex from = ...
QModelIndex to = ...
painter.drawLine(visualRect(from).center(), visualRect(to).center());

kocakden
28th April 2008, 15:40
Thanks again but i couldn't do it again. I found a different way to get the coordinates of cells. Here is my codes:

draw.h:



#ifndef DRAW_H
#define DRAW_H

#include <QWidget>
#include<QTableWidget>

class Draw : public QTableWidget
{
Q_OBJECT
public:
Draw(QWidget *parent = 0);
public slots:
void d_drawline();
};


#endif


draw.cpp:


#include <QTableWidget>
#include <QTableWidgetItem>
#include <QPainter>
#include <QModelIndex>
#include <QAbstractItemView>


#include "draw.h"

Draw::Draw(QWidget *parent)
{
setRowCount(4);
setColumnCount(4);
this->setGeometry(0, 0, 500, 500);
}

void Draw::d_drawline()
{
int x_from = columnViewportPosition(1);
int y_from = rowViewportPosition(1);
int x_to = columnViewportPosition(3);
int y_to = rowViewportPosition(3);

QPainter painter(this);
painter.drawLine(x_from,y_from,x_to, y_to);
}


main.cpp:


#include <QApplication>
#include <QFont>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QVBoxLayout>

#include "draw.h"


class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
};

MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{

QPushButton *d_drawline = new QPushButton(tr("Draw"));
Draw *draw = new Draw;
connect(d_drawline, SIGNAL(clicked()),draw, SLOT(d_drawline()));

QGridLayout *gridLayout = new QGridLayout;
gridLayout->addWidget(d_drawline, 0, 0);
gridLayout->addWidget(draw, 1, 0);
setLayout(gridLayout);

}

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.setGeometry(200, 200, 500, 355);
widget.show();
return app.exec();
}


it builts successfully but nothing changes when i hit to draw button.

wysota
28th April 2008, 17:34
But why didn't you use visualRect()? I gave you practically a complete solution...

kocakden
29th April 2008, 01:16
Because i couldn't understand the QModelIndex class and couldn't use it :((. But what is wrong with my code. While i'm debuging the code, i saw that it returns the coordinates correctly.

wysota
29th April 2008, 10:50
You can use visualItemRect() if you are using convenience classes. Your code is incorrect because it draws outside paint event.

Check out this example. Manipulate the spinboxes to see the effect.