Results 1 to 5 of 5

Thread: Problem with drawing on the Widget

  1. #1
    Join Date
    Oct 2009
    Location
    Poland
    Posts
    34
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Problem with drawing on the Widget

    Hello,
    My problem looks like this: I have two widget in first i draw lines etc and it is child widget situated in parent widget which is the main of application. I have connected those two widget with QSlider, so when i move silder the length of line drawn on child widget should change but that dont happend... i checked and the paintEvent is called on both widget but i cant see result, please healp.

    Best Regards
    kszewczyk

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problem with drawing on the Widget

    Without seeing the code we hardly can help you. Probably you have an error in the connection statement...

  3. #3
    Join Date
    Oct 2009
    Location
    Poland
    Posts
    34
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Problem with drawing on the Widget

    I tested connection, everything is ok becouse i pass some values from one widget to another and variables are changing so that should be ok.

    Regards
    Last edited by kaszewczyk; 16th April 2010 at 06:11.

  4. #4
    Join Date
    Oct 2009
    Location
    Poland
    Posts
    34
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default [SOLVED] Problem with drawing on the Widget

    Hi, sorry for duble post but i cant find what is wrong

    the main window look like this:
    Qt Code:
    1. #include "window.h"
    2.  
    3. Window::Window(QWidget *parent) : QWidget(parent){
    4.  
    5. amplitudeP = 10.0;
    6. ampliSliderPosP = 0;
    7.  
    8. mainLay = new QHBoxLayout;
    9. renderLay = new QVBoxLayout;
    10. optionsLay = new QVBoxLayout;
    11. renderGroupBox = new QGroupBox("Wykresy", this);
    12. optionsGroupBox = new QGroupBox("Opcje", this);
    13.  
    14. oscylo = new Oscyloscop(this);
    15. mightGraph = new SignalMight(this);
    16.  
    17. oscylo->setRenderAreaSize(400 , 400);
    18. oscylo->resizeRenderArea();
    19. mightGraph->setRenderAreaSize(400, 150);
    20. mightGraph->resizeRenderArea();
    21.  
    22. renderLay->addWidget(oscylo);
    23. renderLay->addWidget(mightGraph);
    24. renderGroupBox->setLayout(renderLay);
    25.  
    26. ampliSilder = new QSlider(Qt::Horizontal, this);
    27.  
    28. optionsLay->addWidget(ampliSilder);
    29.  
    30. optionsGroupBox->setLayout(optionsLay);
    31.  
    32. mainLay->addWidget(renderGroupBox);
    33. mainLay->addWidget(optionsGroupBox);
    34. setLayout(mainLay);
    35.  
    36. connect(ampliSilder, SIGNAL(valueChanged(int)), this, SLOT(changeAmplitude(int)));
    37. connect(this, SIGNAL(amplitudeChanget(double)), oscylo, SLOT(reciveAmplitude(double)));
    38.  
    39.  
    40. }
    41.  
    42. Window::~Window(){
    43. delete mainLay;
    44. delete renderLay;
    45. delete optionsLay;
    46. delete oscylo;
    47. delete mightGraph;
    48. }
    49.  
    50. void Window::changeAmplitude(int _pos)
    51. {
    52. if(_pos > ampliSliderPosP)
    53. amplitudeP += 10.0;
    54. else
    55. amplitudeP -=10.0;
    56.  
    57. ampliSliderPosP = _pos;
    58. emit amplitudeChanget(amplitudeP);
    59. }
    60.  
    61. void Window::paintEvent(QPaintEvent *)
    62. {
    63. qDebug("update okna glownego");
    64. }
    To copy to clipboard, switch view to plain text mode 

    the child class look like this:
    Qt Code:
    1. #include "oscyloscop.h"
    2.  
    3. Oscyloscop::Oscyloscop(QWidget* parent) : RenderArea(parent)
    4. {
    5. nullCordP = new Point2D(100, 900);
    6. xEndP = new Point2D(900, 900);
    7. yEndP = new Point2D(100, 100);
    8. signal = new Signal();
    9. amplitudeP = 10.0;
    10.  
    11. }
    12.  
    13. Oscyloscop::~Oscyloscop(){
    14. delete nullCordP;
    15. delete xEndP;
    16. delete yEndP;
    17. }
    18.  
    19. void Oscyloscop::drawCoordSystem(QPen& _pen, QPainter& _painter){
    20. _painter.drawLine(100, 900, 900, 900);
    21. _painter.drawLine(100, 100, 100, 900);
    22. _painter.setFont(QFont("Tahoma", 25));
    23. _painter.drawText(nullCordP->x-6, nullCordP->y+45, "0");
    24. _painter.drawText(450, 980, "Czas [t]");
    25.  
    26. int xDrawPoint = 260;
    27. int yDrawPoint = 740;
    28. _pen.setWidth(5);
    29. _painter.setPen(_pen);
    30. for(int i = 0; i < 5; i++){
    31. _painter.drawLine(xDrawPoint, nullCordP->y, xDrawPoint, nullCordP->y+10);
    32. _painter.drawLine(nullCordP->x-10, yDrawPoint, nullCordP->x, yDrawPoint);
    33. xDrawPoint += 160;
    34. yDrawPoint -= 160;
    35. }
    36.  
    37. _pen.setStyle(Qt::DotLine);
    38. _pen.setWidth(3);
    39. _painter.setPen(_pen);
    40.  
    41. xDrawPoint = 260;
    42. yDrawPoint = 740;
    43. for(int i = 0; i < 5; i++){
    44. _painter.drawLine(xDrawPoint, nullCordP->y, xDrawPoint, nullCordP->y-800);
    45. _painter.drawLine(nullCordP->x, yDrawPoint, nullCordP->x+800, yDrawPoint);
    46. xDrawPoint += 160;
    47. yDrawPoint -= 160;
    48. }
    49. setNumX(2);
    50. float divX = retXscaleDigit().toFloat() / 5.0;
    51. float divXconst = divX;
    52. QString xPrintNum;
    53. xDrawPoint = 260;
    54. for(int i = 0; i < 5; i++){
    55. xPrintNum.setNum(divX);
    56. _painter.drawText(xDrawPoint-6, nullCordP->y+45, xPrintNum);
    57. divX += divXconst;
    58. xDrawPoint += 160;
    59. }
    60. nullCordP->x = 100;
    61. }
    62.  
    63. void Oscyloscop::drawGraph(QPen& _pen, QPainter& _painter){
    64. _pen.setStyle(Qt::SolidLine);
    65. _pen.setColor(Qt::black);
    66. _pen.setWidth(3);
    67. _painter.setPen(_pen);
    68.  
    69.  
    70. samples = signal->generateSignalSin(800, amplitudeP, 5.0, Signal::SIN_2PI5);
    71.  
    72. for(int i = 0 ; i < 800 ; ++i)
    73. {
    74. _painter.drawPoint(nullCordP->x, 500 + (samples[i] + 10));
    75. ++nullCordP->x;
    76.  
    77. }
    78.  
    79. nullCordP->x = 100;
    80.  
    81. }
    82.  
    83. void Oscyloscop::paintEvent(QPaintEvent* _p){
    84. QPainter painter(this);
    85. QPen pen;
    86. QMatrix matrix;
    87. pen.setWidth(10);
    88. pen.setColor(QColor::fromRgb(104, 104, 104, 100));
    89. pen.setStyle(Qt::SolidLine);
    90. painter.setPen(pen);
    91. painter.setRenderHint(QPainter::Antialiasing);
    92. matrix.scale( retWidth() / 1000.0 , retHeight() / 1000.0 );
    93. painter.setMatrix(matrix);
    94. drawCoordSystem(pen, painter);
    95. drawGraph(pen, painter);
    96.  
    97. qDebug("update oscyloscopu");
    98. }
    99.  
    100. void Oscyloscop::reciveAmplitude(double _a)
    101. {
    102. amplitudeP = _a;
    103. update();
    104. }
    To copy to clipboard, switch view to plain text mode 
    and here i make y values for the graph
    Qt Code:
    1. QVector<int> Signal::generateSignalSin(int _period, double _amplitude, double _freq, int _type)
    2. {
    3. double tmp;
    4. QString str;
    5. QStringList strList;
    6. switch(_type)
    7. {
    8. case SIN_2PI5:
    9.  
    10. for(int x = 0 ; x < _period ; ++x)
    11. {
    12. tmp = _amplitude * sin(x * 2.0 * 3.14 * _freq);
    13. str.setNum(tmp);
    14. strList = str.split(".");
    15. str = strList.last();
    16. str.resize(4);
    17. int mod = str.toInt();
    18. if( tmp > 0)
    19. {
    20. if(mod > 5000)
    21. y = ceil(tmp);
    22.  
    23. else
    24. y = floor(tmp);
    25. }
    26. else
    27. {
    28. if(mod > 5000)
    29. y = floor(tmp);
    30. else
    31. y = ceil(tmp);
    32. }
    33. samples.push_back(y);
    34. }
    35.  
    36. break;
    37.  
    38. case SIN_2PI5_2PI10:
    39.  
    40. break;
    41.  
    42. case SIN_2PI5_02_2PI25:
    43.  
    44. break;
    45.  
    46. }// end of switch
    47.  
    48. return samples;
    49. }
    To copy to clipboard, switch view to plain text mode 

    i dont know what might be wrong
    //SOLVED
    i didnt clear up vector with samples
    Last edited by kaszewczyk; 17th April 2010 at 14:24.

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: [SOLVED] Problem with drawing on the Widget

    Just a side node: All the deletes in the dtor of your main window are unnecessary. Qt will delete child widgets automatically (if the inherit QObject). Your layouts get reparent after assigning them to the central layout. So they will be deleted also.

Similar Threads

  1. Replies: 1
    Last Post: 3rd November 2009, 22:26
  2. Drawing a widget in QItemDelegate's paint method
    By darkadept in forum Qt Programming
    Replies: 17
    Last Post: 11th August 2009, 05:15
  3. Replies: 0
    Last Post: 8th July 2009, 11:01
  4. Drawing on a widget created via QDesigner
    By jean in forum Qt Programming
    Replies: 5
    Last Post: 15th January 2009, 13:15
  5. Drawing the expansion dialog widget
    By kaushal_gaurav in forum Qt Programming
    Replies: 1
    Last Post: 25th July 2008, 09:54

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.