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>
{
Q_OBJECT
public:
public slots:
void d_drawline();
};
#endif
#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
To copy to clipboard, switch view to plain text mode
draw.cpp:
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QPainter>
#include <QModelIndex>
#include <QAbstractItemView>
#include "draw.h"
{
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);
painter.drawLine(x_from,y_from,x_to, y_to);
}
#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);
}
To copy to clipboard, switch view to plain text mode
main.cpp:
#include <QApplication>
#include <QFont>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QVBoxLayout>
#include "draw.h"
{
public:
};
MyWidget
::MyWidget(QWidget *parent
){
Draw *draw = new Draw;
connect(d_drawline, SIGNAL(clicked()),draw, SLOT(d_drawline()));
gridLayout->addWidget(d_drawline, 0, 0);
gridLayout->addWidget(draw, 1, 0);
setLayout(gridLayout);
}
int main(int argc, char *argv[])
{
MyWidget widget;
widget.setGeometry(200, 200, 500, 355);
widget.show();
return app.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
it builts successfully but nothing changes when i hit to draw button.
Bookmarks