Results 1 to 14 of 14

Thread: QWidget subclass drawing in wrong place

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default QWidget subclass drawing in wrong place

    Hello all!

    I hava a QWidget subclassed and the painting is not going right. When I call setFluxo all the rects appear in the screen on position y 0

    Qt Code:
    1. #include "fluxocaixawidget.h"
    2.  
    3. #include <QDebug>
    4.  
    5. fluxoCaixaWidget::fluxoCaixaWidget(QWidget *parent) :
    6. QWidget(parent)
    7. {
    8. this->setMouseTracking(true);
    9. dentro = false;
    10. fluxo = NULL;
    11.  
    12. this->update();
    13. }
    14.  
    15. void fluxoCaixaWidget::paintEvent(QPaintEvent *)
    16. {
    17. QPainter painter(this);
    18.  
    19. linhaTempo.setRect(20, this->parentWidget()->height() / 2 - 20, this->parentWidget()->width()-65, 30);
    20.  
    21. QPen caneta;
    22.  
    23. QBrush pincel(Qt::white);
    24. painter.setBrush(pincel);
    25.  
    26. painter.drawRect(parentWidget()->rect());
    27.  
    28. if(this->dentro == true)
    29. {
    30. painter.drawText(QRect(20, 15, linhaTempo.width(), 25), "Linha do tempo", QTextOption::QTextOption(Qt::AlignCenter));
    31. caneta.setWidth(4);
    32. }
    33. else
    34. {
    35. caneta.setWidth(2);
    36. }
    37.  
    38. painter.setPen(caneta);
    39.  
    40. painter.drawRect(linhaTempo);
    41.  
    42. if(fluxo != NULL)
    43. {
    44. for(int i = 0; i < fluxo->count(); i++)
    45. {
    46. if(fluxo->at(i) < 0)
    47. {
    48. caneta.setColor(Qt::red);
    49. caneta.setWidth(2);
    50.  
    51. painter.setPen(caneta);
    52. painter.drawRect(fluxoRect.at(i));
    53. }
    54. else
    55. {
    56. if(fluxo->at(i) == 0)
    57. {
    58. caneta.setColor(Qt::black);
    59. caneta.setWidth(2);
    60.  
    61. painter.setPen(caneta);
    62. painter.drawRect(fluxoRect.at(i));
    63. }
    64. else
    65. {
    66. if(fluxo->at(i) > 0)
    67. {
    68. caneta.setColor(Qt::blue);
    69. caneta.setWidth(2);
    70.  
    71. painter.setPen(caneta);
    72. painter.drawRect(fluxoRect.at(i));
    73. }
    74. }
    75. }
    76. }
    77. }
    78. }
    79.  
    80. void fluxoCaixaWidget::mouseMoveEvent(QMouseEvent *)
    81. {
    82. if(this->linhaTempo.contains(this->mapFromGlobal(QCursor::pos())))
    83. {
    84. dentro = true;
    85. }
    86. else
    87. {
    88. dentro = false;
    89. }
    90. this->update();
    91. }
    92.  
    93. void fluxoCaixaWidget::setFluxo(QList<double> *f)
    94. {
    95. fluxo = f;
    96.  
    97. fluxoRect.clear();
    98.  
    99. int espacamento = 0;
    100.  
    101. if(fluxo->count() > 1)
    102. {
    103. espacamento = linhaTempo.width() / (fluxo->count() - 1);
    104. }
    105.  
    106. for(int i = 0; i < fluxo->count(); i++)
    107. {
    108. if(i == 0)
    109. {
    110. fluxoRect.append(QRect(20, this->parentWidget()->height() / 2 - 20, 10, 50));
    111. }
    112. else
    113. {
    114. fluxoRect.append(QRect(20 + espacamento * i, this->parentWidget()->height() / 2 - 20, 10, 50));
    115. }
    116. }
    117.  
    118. this->update();
    119. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QWidget subclass drawing in wrong place

    Why are you accessing parentWidget()?

    Aside from it not making any sense, what if there is no parent widget?

    Cheers,
    _

  3. #3
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QWidget subclass drawing in wrong place

    Yes, you are right. But how can I have a size reference to create the QRect linhaTempo?. And the QRect linhaTempo must be resizable.

    Thank you for the reply.

    Cheers.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QWidget subclass drawing in wrong place

    Your own widget has a size as well, does it not?

    Cheers,
    _

  5. #5
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QWidget subclass drawing in wrong place

    Yes, it has, but its does not resize when it has a fixed size.

    Could you help me?

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,316
    Thanks
    315
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QWidget subclass drawing in wrong place

    Yes, it has, but its does not resize when it has a fixed size.
    Are you using layouts in the parent widget? If not, this is almost certainly the cause of your widget not resizing. If you put your widget inside a layout on the parent widget, the layout will automatically resize your widget when the parent resizes.

  7. #7
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QWidget subclass drawing in wrong place

    Yes. My widget is inside a layout on the parent widget. But it does not resize

    Qt Code:
    1. QGridLayout *gridLayoutInvestimento5 = new QGridLayout(boxInvestimento3);
    2.  
    3. fluxoCaixaWidget *fluxoCaixa = new fluxoCaixaWidget(boxInvestimento3);
    4.  
    5. boxInvestimento3->setLayout(gridLayoutInvestimento5);
    6. gridLayoutInvestimento5->addWidget(fluxoCaixa);
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,316
    Thanks
    315
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QWidget subclass drawing in wrong place

    Sorry, I can't tell you what is wrong. You aren't showing us enough of your code to understand where your problem is. Six lines of code without any idea of where those lines are being used isn't enough information.

    When I am faced with a problem like this, I create a demo project that simplifies the UI to the most basic level. If the problem still occurs in the demo project, then I figure out what is wrong and I fix it. It is very difficult to diagnose UI problems inside of a much larger project because usually too many other things are happening. Isolate the problem to the most basic parts and fix the problem.

  9. #9
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QWidget subclass drawing in wrong place

    Sorry, its because its a large code. I will put below a larger code

    mainwindow.cpp
    Qt Code:
    1. QWidget *MainWindow::criaEstruturaNovoIvestimento()
    2. {
    3. QWidget *tabInvestimento = new QWidget(this->tabWidgetInvestimento);
    4.  
    5. QGridLayout *gridLayoutInvestimento2 = new QGridLayout(tabInvestimento);
    6. gridLayoutInvestimento2->setMargin(0);
    7. QToolBox *toolboxInvestimento = new QToolBox(tabInvestimento);
    8. gridLayoutInvestimento2->addWidget(toolboxInvestimento);
    9. QWidget *boxInvestimento = new QWidget(toolboxInvestimento);
    10. QWidget *boxInvestimento2 = new QWidget(toolboxInvestimento);
    11. QWidget *boxInvestimento3 = new QWidget(toolboxInvestimento);
    12.  
    13. //SOME CODE
    14.  
    15. //HERE IS THE PROBLEM
    16. //Tool Box 3
    17. QGridLayout *gridLayoutInvestimento5 = new QGridLayout(boxInvestimento3);
    18.  
    19. fluxoCaixaWidget *fluxoCaixa = new fluxoCaixaWidget(boxInvestimento3);
    20.  
    21. boxInvestimento3->setLayout(gridLayoutInvestimento5);
    22. gridLayoutInvestimento5->addWidget(fluxoCaixa);
    23. //MORE CODE BELOW
    24. }
    To copy to clipboard, switch view to plain text mode 

    fluxocaixawidget.cpp
    Qt Code:
    1. #include "fluxocaixawidget.h"
    2.  
    3. #include <QDebug>
    4.  
    5. fluxoCaixaWidget::fluxoCaixaWidget(QWidget *parent) :
    6. QWidget(parent)
    7. {
    8. linhaTempo.setRect(20, 100, 700, 30);
    9.  
    10. this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    11.  
    12.  
    13.  
    14. this->setMouseTracking(true);
    15.  
    16. dentro = false;
    17. fluxo = NULL;
    18. }
    19.  
    20. void fluxoCaixaWidget::paintEvent(QPaintEvent *)
    21. {
    22. QPainter painter(this);
    23.  
    24. //linhaTempo.setRect(20, this->parentWidget()->height() / 2 - 20, this->parentWidget()->width()-65, 30);
    25.  
    26. //linhaTempo.setRect(20, this->parentWidget()->rect().height() / 2 - 20, this->parentWidget()->rect().width()-65, 30);
    27.  
    28. QPen caneta;
    29.  
    30. QBrush pincel(Qt::white);
    31. painter.setBrush(pincel);
    32.  
    33. //painter.drawRect(parentWidget()->rect());
    34. painter.drawRect(0,0,800,600);
    35.  
    36. if(this->dentro == true)
    37. {
    38. painter.drawText(QRect(20, 15, linhaTempo.width(), 25), "Linha do tempo", QTextOption::QTextOption(Qt::AlignCenter));
    39. caneta.setWidth(3);
    40. }
    41. else
    42. {
    43. caneta.setWidth(2);
    44. }
    45.  
    46. painter.setPen(caneta);
    47.  
    48. painter.drawRect(linhaTempo);
    49.  
    50. if(fluxo != NULL)
    51. {
    52. for(int i = 0; i < fluxo->count(); i++)
    53. {
    54. if(fluxo->at(i) < 0)
    55. {
    56. caneta.setColor(Qt::red);
    57. }
    58. else
    59. {
    60. if(fluxo->at(i) == 0)
    61. {
    62. caneta.setColor(Qt::black);
    63. }
    64. else
    65. {
    66. if(fluxo->at(i) > 0)
    67. {
    68. caneta.setColor(Qt::blue);
    69. }
    70. }
    71. }
    72.  
    73. if(fluxoDentro.at(i) == true)
    74. {
    75. QString valor = QString::number(fluxo->at(i));
    76. painter.drawText(QRect(20, 15, linhaTempo.width(), 25), valor, QTextOption::QTextOption(Qt::AlignCenter));
    77. caneta.setWidth(3);
    78. }
    79. else
    80. {
    81. caneta.setWidth(2);
    82. }
    83. painter.setPen(caneta);
    84. painter.drawRect(fluxoRect.at(i));
    85. }
    86. }
    87. }
    88.  
    89. void fluxoCaixaWidget::mouseMoveEvent(QMouseEvent *)
    90. {
    91. if(linhaTempo.contains(this->mapFromGlobal(QCursor::pos())))
    92. {
    93. dentro = true;
    94. }
    95. else
    96. {
    97. dentro = false;
    98. }
    99.  
    100. for(int i = 0; i < fluxoRect.count(); i++)
    101. {
    102. if(fluxoRect.at(i).contains(this->mapFromGlobal(QCursor::pos())))
    103. {
    104. fluxoDentro.insert(i, true);
    105. dentro = false;
    106. }
    107. else
    108. {
    109. fluxoDentro.insert(i, false);
    110. }
    111. }
    112.  
    113. this->update();
    114. }
    115.  
    116. void fluxoCaixaWidget::setFluxo(QList<double> *f)
    117. {
    118. fluxo = f;
    119.  
    120. fluxoRect.clear();
    121. fluxoDentro.clear();
    122.  
    123. int espacamento = 0;
    124.  
    125. if(fluxo->count() > 1)
    126. {
    127. espacamento = linhaTempo.width() / (fluxo->count() - 1);
    128. }
    129.  
    130. for(int i = 0; i < fluxo->count(); i++)
    131. {
    132. if(i == 0)
    133. {
    134. //fluxoRect.append(QRect(20, this->parentWidget()->height() / 2 - 20, 10, 50));
    135. fluxoRect.append(QRect(20, linhaTempo.y(), 10, 50));
    136. fluxoDentro.append(false);
    137. }
    138. else
    139. {
    140. //fluxoRect.append(QRect(20 + espacamento * i, this->parentWidget()->height() / 2 - 20, 10, 50));
    141. fluxoRect.append(QRect(20 + espacamento * i, linhaTempo.y(), 10, 50));
    142. fluxoDentro.append(false);
    143. }
    144. }
    145.  
    146. this->update();
    147. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QWidget subclass drawing in wrong place

    when I do that, the QPushButton resizes but not my widget (fluxocaixawidget)
    Qt Code:
    1. //Tool Box 3
    2. QGridLayout *gridLayoutInvestimento5 = new QGridLayout(boxInvestimento3);
    3.  
    4. fluxoCaixaWidget *fluxoCaixa = new fluxoCaixaWidget(boxInvestimento3);
    5.  
    6. QPushButton *button = new QPushButton("Teste", boxInvestimento3);
    7. QVBoxLayout *vertical = new QVBoxLayout(boxInvestimento3);
    8. vertical->addWidget(button);
    9. vertical->addWidget(fluxoCaixa);
    10.  
    11. //gridLayoutInvestimento5->addWidget(fluxoCaixa);
    12.  
    13. gridLayoutInvestimento5->addLayout(vertical,0,0);
    14. boxInvestimento3->setLayout(gridLayoutInvestimento5);
    15.  
    16. //------------------------------------------------------------------------
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,316
    Thanks
    315
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QWidget subclass drawing in wrong place

    Your "fluxoCaixaWidget" is derived from QWidget, right? Have you reimplemented the QWidget methods that are used by layouts to determine what size your widget is supposed to be?

    In particular, methods like QWidget::minimumSize(), QWidget::minimumSizeHint(), and QWidget::sizeHint()?

Similar Threads

  1. Replies: 4
    Last Post: 19th June 2013, 11:08
  2. Replies: 3
    Last Post: 25th September 2012, 20:11
  3. When is the geometry of a QWidget subclass set?
    By Luc4 in forum Qt Programming
    Replies: 3
    Last Post: 3rd May 2011, 21:14
  4. Replies: 4
    Last Post: 17th January 2011, 13:05
  5. Replies: 2
    Last Post: 1st January 2008, 13:31

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.