Results 1 to 3 of 3

Thread: Transfer paint event to QScrollarea

  1. #1
    Join Date
    Apr 2012
    Posts
    49
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Transfer paint event to QScrollarea

    Hi all,

    I am trying to draw a graph in QT application using a paintevent.

    I could be able to draw in the wisget. But i need to transfer that paintevent to QScroll Area that is present in MainWindow.h.

    Could anyone help me to sort out thius issue.Here i am pasting my code for your reference.


    main.cpp

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QtGui>
    4. #include<QPainter>
    5.  
    6.  
    7. #define XAXISLABEL 340
    8. #define XAXISSTARTWIDTH 100 //start width from the left corner
    9. #define XAXISSTDWIDTH 40 //width betwen each label in the x axis
    10. #define XAXISCORRECTION 20 //correction value for the xaxis lable display
    11.  
    12.  
    13. MainWindow::MainWindow(QWidget *parent) :
    14. QMainWindow(parent),
    15. ui(new Ui::MainWindow)
    16. {
    17. ui->setupUi(this);
    18. }
    19.  
    20. MainWindow::~MainWindow()
    21. {
    22. delete ui;
    23. }
    24.  
    25. void MainWindow::paintEvent(QPaintEvent * /*event*/)
    26. {
    27. QPainter painter(this);
    28. // painter.translate(scrollArea);
    29. // painter.setWindow(scrollArea);
    30. int i=1000;
    31.  
    32. const int start = 100;
    33. const int act_unit_w = 1;
    34. const int act_unit_h = 10;
    35.  
    36. QFontMetrics metrics(font());
    37. QColor color;
    38.  
    39. QBrush Brush1(Qt::red,Qt::SolidPattern);
    40. QBrush Brush2(Qt::green,Qt::SolidPattern);
    41.  
    42. int ReferenceValue[22]={12,44,55,66,77,88,99,11,10,13,78,87,66,56,76,67,89,67,56,67,67};
    43. int PCValue[22]={12,44,40,20,77,88,32,11,10,13,78,87,66,56,76,67,89,67,56,67,67};
    44.  
    45. int myvalue=20;
    46. int refvalue=40;
    47. int MyTemp1=25;
    48. int MyTemp2=40;
    49.  
    50. int width=15;
    51. int height=3;
    52.  
    53. for(int k=0;k<21;k++)
    54. {
    55. myvalue=ReferenceValue[k];
    56. painter.fillRect((XAXISSTARTWIDTH+MyTemp1),(320-(myvalue*height)),width,(myvalue*height),Brush1);
    57. MyTemp1=MyTemp1+40;
    58.  
    59. }
    60.  
    61. for(int m=0;m<21;m++)
    62. {
    63. refvalue=PCValue[m];
    64. painter.fillRect((XAXISSTARTWIDTH+MyTemp2),(320-(refvalue*height)),width,(refvalue*height),Brush2);
    65. MyTemp2=MyTemp2+40;
    66.  
    67. }
    68. painter.save();
    69. // painter.setBrush(badBrush);
    70. painter.setPen(QColor(Qt::black));
    71.  
    72.  
    73. //fill x axis
    74. for( int j=start; j< 1000+start ; j += act_unit_w*XAXISSTDWIDTH ){
    75. painter.drawLine(j,320,j,325);
    76. // painter.drawText(j,340,QString::number(j-start,10));
    77. }
    78.  
    79. //X Axis text
    80.  
    81. painter.drawText((XAXISSTARTWIDTH +0)-XAXISCORRECTION,XAXISLABEL,QString("0"));
    82. painter.drawText((XAXISSTARTWIDTH +(XAXISSTDWIDTH *1))-XAXISCORRECTION,XAXISLABEL,QString("3-4"));
    83. painter.drawText((XAXISSTARTWIDTH +(XAXISSTDWIDTH *2))-XAXISCORRECTION,XAXISLABEL,QString("4-5"));
    84. painter.drawText((XAXISSTARTWIDTH +(XAXISSTDWIDTH *3))-XAXISCORRECTION,XAXISLABEL,QString("5-6"));
    85. painter.drawText((XAXISSTARTWIDTH +(XAXISSTDWIDTH *4))-XAXISCORRECTION,XAXISLABEL,QString("6-7"));
    86. painter.drawText((XAXISSTARTWIDTH +(XAXISSTDWIDTH *5))-XAXISCORRECTION,XAXISLABEL,QString("7-8"));
    87. painter.drawText((XAXISSTARTWIDTH +(XAXISSTDWIDTH *6))-XAXISCORRECTION,XAXISLABEL,QString("8-9"));
    88. painter.drawText((XAXISSTARTWIDTH +(XAXISSTDWIDTH *7))-XAXISCORRECTION,XAXISLABEL,QString("9-10"));
    89. painter.drawText((XAXISSTARTWIDTH +(XAXISSTDWIDTH *8))-XAXISCORRECTION,XAXISLABEL,QString("10-11"));
    90. painter.drawText((XAXISSTARTWIDTH +(XAXISSTDWIDTH *9))-XAXISCORRECTION,XAXISLABEL,QString("11-12"));
    91. painter.drawText((XAXISSTARTWIDTH +(XAXISSTDWIDTH *10))-XAXISCORRECTION,XAXISLABEL,QString("12-13"));
    92. painter.drawText((XAXISSTARTWIDTH +(XAXISSTDWIDTH *11))-XAXISCORRECTION,XAXISLABEL,QString("13-14"));
    93. painter.drawText((XAXISSTARTWIDTH +(XAXISSTDWIDTH *12))-XAXISCORRECTION,XAXISLABEL,QString("14-15"));
    94. painter.drawText((XAXISSTARTWIDTH +(XAXISSTDWIDTH *13))-XAXISCORRECTION,XAXISLABEL,QString("15-16"));
    95. painter.drawText((XAXISSTARTWIDTH +(XAXISSTDWIDTH *14))-XAXISCORRECTION,XAXISLABEL,QString("16-17"));
    96. painter.drawText((XAXISSTARTWIDTH +(XAXISSTDWIDTH *15))-XAXISCORRECTION,XAXISLABEL,QString("17-18"));
    97. painter.drawText((XAXISSTARTWIDTH +(XAXISSTDWIDTH *16))-XAXISCORRECTION,XAXISLABEL,QString("18-19"));
    98. painter.drawText((XAXISSTARTWIDTH +(XAXISSTDWIDTH *17))-XAXISCORRECTION,XAXISLABEL,QString("19-XAXISCORRECTION"));
    99. painter.drawText((XAXISSTARTWIDTH +(XAXISSTDWIDTH *18))-XAXISCORRECTION,XAXISLABEL,QString("XAXISCORRECTION-21"));
    100. painter.drawText((XAXISSTARTWIDTH +(XAXISSTDWIDTH *19))-XAXISCORRECTION,XAXISLABEL,QString("21-22"));
    101. painter.drawText((XAXISSTARTWIDTH +(XAXISSTDWIDTH *20))-XAXISCORRECTION,XAXISLABEL,QString("22-23"));
    102. painter.drawText((XAXISSTARTWIDTH +(XAXISSTDWIDTH *21))-XAXISCORRECTION,XAXISLABEL,QString("23-24"));
    103.  
    104. //Y axis text
    105. painter.drawText(50,20,QString("100"));
    106. painter.drawText(50,80,QString("80"));
    107. painter.drawText(50,140,QString("60"));
    108. painter.drawText(50,200,QString("40"));
    109. painter.drawText(50,260,QString("20"));
    110. painter.drawLine(start,20,1000,20);
    111. painter.drawLine(start,80,1000,80);
    112. painter.drawLine(start,140,1000,140);
    113. painter.drawLine(start,200,1000,200);
    114. painter.drawLine(start,260,1000,260);
    115. // painter.drawText(0,0,640,50,Qt::AlignCenter,QString("Power Curve"));
    116. painter.drawLine(start,20,start,320);
    117. painter.drawLine(start,320,1000,320);
    118.  
    119. painter.restore();
    120. }
    To copy to clipboard, switch view to plain text mode 


    mainwindow.h

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QScrollArea>
    6.  
    7. namespace Ui {
    8. class MainWindow;
    9. }
    10.  
    11. class MainWindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit MainWindow(QWidget *parent = 0);
    17. ~MainWindow();
    18. public slots:
    19.  
    20. protected:
    21. void paintEvent(QPaintEvent *event);
    22. QSize size;
    23. //void timerEvent(QTimerEvent *event);
    24.  
    25. public:
    26. QScrollArea *scrollArea;
    27.  
    28.  
    29. private:
    30. Ui::MainWindow *ui;
    31. };
    32.  
    33. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 



    mainwindow.ui

    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <ui version="4.0">
    3. <class>MainWindow</class>
    4. <widget class="QMainWindow" name="MainWindow">
    5. <property name="geometry">
    6. <rect>
    7. <x>0</x>
    8. <y>0</y>
    9. <width>1024</width>
    10. <height>600</height>
    11. </rect>
    12. </property>
    13. <property name="windowTitle">
    14. <string>MainWindow</string>
    15. </property>
    16. <widget class="QWidget" name="centralWidget">
    17. <widget class="QScrollArea" name="scrollArea">
    18. <property name="geometry">
    19. <rect>
    20. <x>60</x>
    21. <y>60</y>
    22. <width>451</width>
    23. <height>291</height>
    24. </rect>
    25. </property>
    26. <property name="minimumSize">
    27. <size>
    28. <width>451</width>
    29. <height>291</height>
    30. </size>
    31. </property>
    32. <property name="maximumSize">
    33. <size>
    34. <width>451</width>
    35. <height>291</height>
    36. </size>
    37. </property>
    38. <property name="widgetResizable">
    39. <bool>true</bool>
    40. </property>
    41. <widget class="QWidget" name="scrollAreaWidgetContents">
    42. <property name="geometry">
    43. <rect>
    44. <x>0</x>
    45. <y>0</y>
    46. <width>449</width>
    47. <height>289</height>
    48. </rect>
    49. </property>
    50. </widget>
    51. </widget>
    52. </widget>
    53. <widget class="QMenuBar" name="menuBar">
    54. <property name="geometry">
    55. <rect>
    56. <x>0</x>
    57. <y>0</y>
    58. <width>1024</width>
    59. <height>25</height>
    60. </rect>
    61. </property>
    62. </widget>
    63. <widget class="QToolBar" name="mainToolBar">
    64. <attribute name="toolBarArea">
    65. <enum>TopToolBarArea</enum>
    66. </attribute>
    67. <attribute name="toolBarBreak">
    68. <bool>false</bool>
    69. </attribute>
    70. </widget>
    71. <widget class="QStatusBar" name="statusBar"/>
    72. </widget>
    73. <layoutdefault spacing="6" margin="11"/>
    74. <resources/>
    75. <connections/>
    76. </ui>
    To copy to clipboard, switch view to plain text mode 


    Thanks in Advance.

    Regards
    Vinithr
    Last edited by high_flyer; 12th August 2012 at 17:40.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Transfer paint event to QScrollarea

    Create a custom widget that has your painting code and make an instance of that widget the central widget of a generic QMainWindow.

  3. #3
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Transfer paint event to QScrollarea

    If u r trying to create graphs , u can use qwt
    qwt.sourceforge.net

    hope it helps
    Bala

Similar Threads

  1. Delegate paint event bug?
    By jerry7 in forum Qt Programming
    Replies: 2
    Last Post: 20th October 2010, 02:14
  2. keyboard event transfer
    By vijayabcdef in forum Qt Programming
    Replies: 3
    Last Post: 22nd July 2010, 06:54
  3. setPixmap and Paint Event
    By Magu in forum Newbie
    Replies: 7
    Last Post: 11th March 2010, 10:32
  4. Timer event & paint event, priority
    By Teuniz in forum Qt Programming
    Replies: 0
    Last Post: 2nd February 2010, 13:33
  5. Paint event function in key press event
    By soumya in forum Qt Programming
    Replies: 6
    Last Post: 2nd February 2010, 12:40

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
  •  
Qt is a trademark of The Qt Company.