PDA

View Full Version : How to draw 2D graph in a created secondary window



ssaguiar
28th April 2011, 00:47
I have created an app wich take cares of showing somw data that cames from rs-232 port.
The data is shown using 16 lcd numbers.
When the user click over a small image near every lcdnumber (button with icon), I create another window, wich receives the objectName of the main window's lcdnumber wich button was clicked.
I use signalMapper to do this.
The problem is that I need to plot the graph of the data referent to this tank, in the secondary created window and can't get how to make it work as the draw system must use the paintEvent to start.

This is my code:


.
.

QSignalMapper *signalMapper= new QSignalMapper(this);
.
.

View_tank1 = new QPushButton( "", this );
View_tank1->setFlat(true);
View_tank1->setIcon (ButtonIcon);
View_tank1->setGeometry( QRect( 160, 10, 16, 16 ));
View_tank1->setCursor(Qt::ArrowCursor);
View_tank1->setObjectName("Tanque_1");
View_tank1->setCursor(Qt::OpenHandCursor);
View_tank1->setStatusTip(tr("Detalhes do tanque."));
connect(View_tank1, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(View_tank1, View_tank1->objectName());// ->text());

View_tank2 = new QPushButton( "", this );
View_tank2->setFlat(true);
View_tank2->setIcon (ButtonIcon);
View_tank2->setGeometry( QRect( 350, 10, 16, 16 ));
View_tank2->setCursor(Qt::ArrowCursor);
View_tank2->setObjectName("Tanque_2");
View_tank2->setCursor(Qt::OpenHandCursor);
connect(View_tank2, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(View_tank2, View_tank2->objectName());
.
.
.
View_tank15 = new QPushButton( "", this );
View_tank15->setFlat(true);
View_tank15->setIcon (ButtonIcon);
View_tank15->setGeometry( QRect( 550, 340, 16, 16 ));
View_tank15->setCursor(Qt::ArrowCursor);
View_tank15->setObjectName("Tanque_15");
View_tank15->setCursor(Qt::OpenHandCursor);
connect(View_tank15, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(View_tank15, View_tank15->objectName());

View_tank16 = new QPushButton( "", this );
View_tank16->setFlat(true);
View_tank16->setIcon (ButtonIcon);
View_tank16->setGeometry( QRect( 760, 340, 16, 16 ));
View_tank16->setCursor(Qt::ArrowCursor);
View_tank16->setObjectName("Tanque_16");
View_tank16->setCursor(Qt::OpenHandCursor);
connect(View_tank16, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(View_tank16, View_tank16->objectName());

connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(View_tank_Details(QString)));
.
.
.

void MainWindow::View_tank_Details(QString local)
{

QIcon ico;
ico.addPixmap(QPixmap(":/images/exit-2.svg"), QIcon::Normal, QIcon::On);
ico.addPixmap(QPixmap(":/images/exit.svg"), QIcon::Normal, QIcon::Off);

QDialog *Tank_Details = new QDialog();
QLabel *GraphPlot = new QLabel(Tank_Details);
QLabel *GraphTitle = new QLabel(Tank_Details);
QPushButton *ExitButton = new QPushButton("", GraphTitle);
QLCDNumber *Tanque1Level = new QLCDNumber(5, GraphTitle );

QPen pen(Qt::black, 2, Qt::SolidLine);
QPainter painter(GraphPlot);

Tank_Details->setWindowFlags(Tank_Details->windowFlags() & Qt::Window | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint);
Tank_Details->setGeometry(x,y,800,480);
Tank_Details->show();
Tank_Details->activateWindow();
Tank_Details->setStyleSheet( "background-image: url(:/images/background_main.png)");

Tanque1Level->setGeometry( 10, 5, 180, 50 );
Tanque1Level->setSmallDecimalPoint( false );
Tanque1Level->setNumDigits( 5 );
Tanque1Level->setSegmentStyle(QLCDNumber::Flat);
Tanque1Level->setFrameShape(QLCDNumber::NoFrame);
Tanque1Level->setFrameShadow(QLCDNumber::Plain);
Tanque1Level->setMode(QLCDNumber::Dec);
Tanque1Level->setStyleSheet("background: transparent;color:rgb(0,255,0); border: 0px;");
Tanque1Level->display(12976);

GraphTitle->setGeometry(10, 20, 780, 60);
//set font
QFont font;
font.setPointSize(32);
font.setBold(true);
GraphTitle->setFont(font);
GraphTitle->setAutoFillBackground(true);
GraphTitle->setAlignment( Qt::AlignCenter );
GraphTitle->setFrameStyle( QFrame::Panel | QFrame::Raised );
GraphTitle->setStyleSheet("color:rgb(255,255,255);border: 1px solid grey;");
GraphTitle->setText(local);

GraphPlot->setGeometry(10, 90, 780, 380);
GraphPlot->setStyleSheet("border: 1px solid grey;");

ExitButton->setIcon(QIcon(ico));
ExitButton->setText(tr(""));
ExitButton->setFlat(true);
//ExitButton->setAutoFillBackground(true);
ExitButton->setIconSize(QSize(32,32));
ExitButton->setGeometry( QRect( 740, 14, 32, 32));
ExitButton->setCursor(Qt::OpenHandCursor);
ExitButton->setStyleSheet("background: transparent;border: 0px;");

connect( ExitButton, SIGNAL( clicked() ), Tank_Details, SLOT( close() ) );

painter.setPen(pen);
painter.drawLine(20, 40, 250, 40);

pen.setStyle(Qt::DashLine);
painter.setPen(pen);
painter.drawLine(20, 80, 250, 80);

pen.setStyle(Qt::DashDotLine);
painter.setPen(pen);
painter.drawLine(20, 120, 250, 120);

pen.setStyle(Qt::DotLine);
painter.setPen(pen);
painter.drawLine(20, 160, 250, 160);

pen.setStyle(Qt::DashDotDotLine);
painter.setPen(pen);
painter.drawLine(20, 200, 250, 200);


QVector<qreal> dashes;
qreal space = 4;

dashes << 1 << space << 5 << space;

pen.setStyle(Qt::CustomDashLine);
pen.setDashPattern(dashes);
painter.setPen(pen);
painter.drawLine(20, 240, 250, 240);

}

stampede
2nd May 2011, 10:12
Try to create separate class GraphPlot and reimplement paintEvent, mixing the code like that can lead only to serious headache. You have layout and geometry management, pen and painting setup, everything in one method. This code will be extremely hard to maintain and extend if you continue coding like that.