PDA

View Full Version : From QTableWidget to QPainter



hichemnho
7th February 2012, 10:24
Hello,

I would like to fill a QTableWidget , and after clicking on the button to validate , paint lines between circles according to the condition that if the element[i][j] of the qtablewidget equals to 1 then there will be a line between the circle i and the circle j.
Here is the code:




//arbre.h
#ifndef ARBRE_H
#define ARBRE_H
#include <QtGui>

class Arbre : public QWidget
{
Q_OBJECT

public:
Arbre();

private slots:

void dessin ();
private:
QTableWidget* tableWidget;
QPushButton * Tracer;
QPainter *painter;
int M [2][2];
//void paintEvent(QPaintEvent *);
};



#endif // ARBRE_H

//arbre.cpp
#include "arbre.h"
#include "math.h"
#include <QtGui>
Arbre::Arbre()
{
tableWidget = new QTableWidget(2,2,this);
Tracer = new QPushButton("Tracer",this);
Tracer->move(10,10);

connect(Tracer, SIGNAL(clicked()), this, SLOT(dessin()));
}


/*void Arbre::paintEvent(QPaintEvent *e)
{
tableWidget = new QTableWidget(2,2,this);
Tracer = new QPushButton("Tracer",this);
Tracer->move(10,10);

connect(Tracer, SIGNAL(clicked()), this, SLOT(dessin()));
}*/
/////////////////////////////////////////////////////////////////



void Arbre::dessin()
{
int Nb_Sommet=2;

int s;
int k;
QString str;
for (s=0; s<Nb_Sommet; s++)
{
for (k=0; k<Nb_Sommet; k++)
{
M[s][k] = 0;

}
}
str = ((tableWidget->itemAt(1,0))->text());
int dec = str.toInt();
(M[0][1])=dec;
QPainter painter(this);

bool tour=false;
int x=35;
int y=45;
int Num_Sommet=0;

int T_Coordonnees [Nb_Sommet][Nb_Sommet];
while (Num_Sommet<Nb_Sommet)
{

painter.drawEllipse(x,y,30,30);
(T_Coordonnees[Num_Sommet][0])=x;
(T_Coordonnees[Num_Sommet][1])=y;


if (tour==false)
{
x+=85;
tour=true;
}
else
{
y+=95;
tour=false;
}
Num_Sommet++;
}
Num_Sommet--;
int i,j;
for (i=0; i<Nb_Sommet; i++){
for (j=0; j<Nb_Sommet; j++){
if( (M[i][j] == 1)&&(M[j][i]==1) )
{


painter.drawLine((T_Coordonnees[i][0]+20),(T_Coordonnees[i][1]),(T_Coordonnees[j][0]+20),(T_Coordonnees[j][1]));
if( (T_Coordonnees[i][1]>45)||(T_Coordonnees[j][1]>45) )
{
painter.drawLine(((T_Coordonnees[i][0]+20)-10),(T_Coordonnees[i][1]),((T_Coordonnees[j][0]+20)-10),(T_Coordonnees[j][1]));
}
else
{
painter.drawLine((T_Coordonnees[i][0]+20),(T_Coordonnees[i][1]+30),(T_Coordonnees[j][0]+20),(T_Coordonnees[j][1]+30));
}
}
else
{
if (M[i][j]==1)
{
painter.drawLine((T_Coordonnees[i][0]+20),(T_Coordonnees[i][1]),(T_Coordonnees[j][0]+20),(T_Coordonnees[j][1]));
}

}

}
}



}


//main.cpp

#include <QApplication>
#include "arbre.h"

int main(int argc, char* argv[])
{
QApplication app(argc, argv);

Arbre fenetre;
fenetre.show();

return app.exec();
}

Thank you in advance for your time and attention.

wysota
7th February 2012, 11:41
You can only paint from within a paintEvent(). Calculate your stuff, call update() (which will eventually call paintEvent()) and do the painting in the event handler.

ChrisW67
7th February 2012, 21:48
See the earlier thread for more on this same problem.