Results 1 to 14 of 14

Thread: QWidget subclass drawing in wrong place

  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,230
    Thanks
    302
    Thanked 864 Times in 851 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,230
    Thanks
    302
    Thanked 864 Times in 851 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,230
    Thanks
    302
    Thanked 864 Times in 851 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()?

  12. #12
    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

    Hello d_stranz

    First of all, thanks for the help

    It is my first custom widget and I am not figuring out how I need to implement this methods. Can you help me with some code?

  13. #13
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QWidget subclass drawing in wrong place

    The most important thing is for your custom widget to return a reasonable sizeHint(). How big do you want it to be when it is first shown? 100 x 100, 200 x 200? Implement QWidget::sizeHint() to return QSize( 100, 100 ) or something sensible.

    Do you want to items drawn inside your custom widget (in the paintEvent()) to change position as the widget is resized by the layout? Then you either need to customize the drawing (in paintEvent()) or change the position and size of the QGraphicsScene (in resizeEvent()) to make that happen.

    There are many, many examples of writing custom widgets on the Web. Just Google for "custom QWidget example". The Analog Clock from the Qt documentation is a good place to start. It scales the clock size in the paintEvent() as the window size changes.

  14. #14
    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

    SOLVED

    Thank you d_stranz for yout replies and time

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

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.