Results 1 to 2 of 2

Thread: How to draw 2D graph in a created secondary window

  1. #1
    Join Date
    Apr 2011
    Posts
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default How to draw 2D graph in a created secondary window

    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:
    Qt Code:
    1. .
    2. .
    3.  
    4. QSignalMapper *signalMapper= new QSignalMapper(this);
    5. .
    6. .
    7.  
    8. View_tank1 = new QPushButton( "", this );
    9. View_tank1->setFlat(true);
    10. View_tank1->setIcon (ButtonIcon);
    11. View_tank1->setGeometry( QRect( 160, 10, 16, 16 ));
    12. View_tank1->setCursor(Qt::ArrowCursor);
    13. View_tank1->setObjectName("Tanque_1");
    14. View_tank1->setCursor(Qt::OpenHandCursor);
    15. View_tank1->setStatusTip(tr("Detalhes do tanque."));
    16. connect(View_tank1, SIGNAL(clicked()), signalMapper, SLOT(map()));
    17. signalMapper->setMapping(View_tank1, View_tank1->objectName());// ->text());
    18.  
    19. View_tank2 = new QPushButton( "", this );
    20. View_tank2->setFlat(true);
    21. View_tank2->setIcon (ButtonIcon);
    22. View_tank2->setGeometry( QRect( 350, 10, 16, 16 ));
    23. View_tank2->setCursor(Qt::ArrowCursor);
    24. View_tank2->setObjectName("Tanque_2");
    25. View_tank2->setCursor(Qt::OpenHandCursor);
    26. connect(View_tank2, SIGNAL(clicked()), signalMapper, SLOT(map()));
    27. signalMapper->setMapping(View_tank2, View_tank2->objectName());
    28. .
    29. .
    30. .
    31. View_tank15 = new QPushButton( "", this );
    32. View_tank15->setFlat(true);
    33. View_tank15->setIcon (ButtonIcon);
    34. View_tank15->setGeometry( QRect( 550, 340, 16, 16 ));
    35. View_tank15->setCursor(Qt::ArrowCursor);
    36. View_tank15->setObjectName("Tanque_15");
    37. View_tank15->setCursor(Qt::OpenHandCursor);
    38. connect(View_tank15, SIGNAL(clicked()), signalMapper, SLOT(map()));
    39. signalMapper->setMapping(View_tank15, View_tank15->objectName());
    40.  
    41. View_tank16 = new QPushButton( "", this );
    42. View_tank16->setFlat(true);
    43. View_tank16->setIcon (ButtonIcon);
    44. View_tank16->setGeometry( QRect( 760, 340, 16, 16 ));
    45. View_tank16->setCursor(Qt::ArrowCursor);
    46. View_tank16->setObjectName("Tanque_16");
    47. View_tank16->setCursor(Qt::OpenHandCursor);
    48. connect(View_tank16, SIGNAL(clicked()), signalMapper, SLOT(map()));
    49. signalMapper->setMapping(View_tank16, View_tank16->objectName());
    50.  
    51. connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(View_tank_Details(QString)));
    52. .
    53. .
    54. .
    55.  
    56. void MainWindow::View_tank_Details(QString local)
    57. {
    58.  
    59. QIcon ico;
    60. ico.addPixmap(QPixmap(":/images/exit-2.svg"), QIcon::Normal, QIcon::On);
    61. ico.addPixmap(QPixmap(":/images/exit.svg"), QIcon::Normal, QIcon::Off);
    62.  
    63. QDialog *Tank_Details = new QDialog();
    64. QLabel *GraphPlot = new QLabel(Tank_Details);
    65. QLabel *GraphTitle = new QLabel(Tank_Details);
    66. QPushButton *ExitButton = new QPushButton("", GraphTitle);
    67. QLCDNumber *Tanque1Level = new QLCDNumber(5, GraphTitle );
    68.  
    69. QPen pen(Qt::black, 2, Qt::SolidLine);
    70. QPainter painter(GraphPlot);
    71.  
    72. Tank_Details->setWindowFlags(Tank_Details->windowFlags() & Qt::Window | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint);
    73. Tank_Details->setGeometry(x,y,800,480);
    74. Tank_Details->show();
    75. Tank_Details->activateWindow();
    76. Tank_Details->setStyleSheet( "background-image: url(:/images/background_main.png)");
    77.  
    78. Tanque1Level->setGeometry( 10, 5, 180, 50 );
    79. Tanque1Level->setSmallDecimalPoint( false );
    80. Tanque1Level->setNumDigits( 5 );
    81. Tanque1Level->setSegmentStyle(QLCDNumber::Flat);
    82. Tanque1Level->setFrameShape(QLCDNumber::NoFrame);
    83. Tanque1Level->setFrameShadow(QLCDNumber::Plain);
    84. Tanque1Level->setMode(QLCDNumber::Dec);
    85. Tanque1Level->setStyleSheet("background: transparent;color:rgb(0,255,0); border: 0px;");
    86. Tanque1Level->display(12976);
    87.  
    88. GraphTitle->setGeometry(10, 20, 780, 60);
    89. //set font
    90. QFont font;
    91. font.setPointSize(32);
    92. font.setBold(true);
    93. GraphTitle->setFont(font);
    94. GraphTitle->setAutoFillBackground(true);
    95. GraphTitle->setAlignment( Qt::AlignCenter );
    96. GraphTitle->setFrameStyle( QFrame::Panel | QFrame::Raised );
    97. GraphTitle->setStyleSheet("color:rgb(255,255,255);border: 1px solid grey;");
    98. GraphTitle->setText(local);
    99.  
    100. GraphPlot->setGeometry(10, 90, 780, 380);
    101. GraphPlot->setStyleSheet("border: 1px solid grey;");
    102.  
    103. ExitButton->setIcon(QIcon(ico));
    104. ExitButton->setText(tr(""));
    105. ExitButton->setFlat(true);
    106. //ExitButton->setAutoFillBackground(true);
    107. ExitButton->setIconSize(QSize(32,32));
    108. ExitButton->setGeometry( QRect( 740, 14, 32, 32));
    109. ExitButton->setCursor(Qt::OpenHandCursor);
    110. ExitButton->setStyleSheet("background: transparent;border: 0px;");
    111.  
    112. connect( ExitButton, SIGNAL( clicked() ), Tank_Details, SLOT( close() ) );
    113.  
    114. painter.setPen(pen);
    115. painter.drawLine(20, 40, 250, 40);
    116.  
    117. pen.setStyle(Qt::DashLine);
    118. painter.setPen(pen);
    119. painter.drawLine(20, 80, 250, 80);
    120.  
    121. pen.setStyle(Qt::DashDotLine);
    122. painter.setPen(pen);
    123. painter.drawLine(20, 120, 250, 120);
    124.  
    125. pen.setStyle(Qt::DotLine);
    126. painter.setPen(pen);
    127. painter.drawLine(20, 160, 250, 160);
    128.  
    129. pen.setStyle(Qt::DashDotDotLine);
    130. painter.setPen(pen);
    131. painter.drawLine(20, 200, 250, 200);
    132.  
    133.  
    134. QVector<qreal> dashes;
    135. qreal space = 4;
    136.  
    137. dashes << 1 << space << 5 << space;
    138.  
    139. pen.setStyle(Qt::CustomDashLine);
    140. pen.setDashPattern(dashes);
    141. painter.setPen(pen);
    142. painter.drawLine(20, 240, 250, 240);
    143.  
    144. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to draw 2D graph in a created secondary window

    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.

Similar Threads

  1. Replies: 4
    Last Post: 21st February 2011, 14:28
  2. Replies: 1
    Last Post: 12th June 2009, 09:26
  3. Debuging a secondary thread
    By ^Nisok^ in forum Newbie
    Replies: 5
    Last Post: 29th April 2009, 08:14
  4. Replies: 5
    Last Post: 30th March 2008, 16:53
  5. Replies: 5
    Last Post: 7th November 2006, 15:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.