Results 1 to 6 of 6

Thread: setCellWidget - strange behavior

  1. #1
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default setCellWidget - strange behavior

    I created a Class based on QWidget. This Class represents the element i want to use as a QTableWidget element. I use setCellWidget to set this item. The strange behavior I'm talking about: when i set this custom widget to the 0, 0 cell - everything work fine. If i set this widget to the 0, 1 cell (0 row, 1 column), the things I've drawn in custom Widget are shifting to the right. If I set this widget to the 1, 0 cell, then it shifts to the bottom... Here are some pictures as well as the relevant code:

    WeekCalendarWidget.cpp
    Qt Code:
    1. #include "weekcalendarwidget.h"
    2.  
    3. WeekCalendarWidget::WeekCalendarWidget(QWidget *parent) :
    4. QTableWidget(parent)
    5. {
    6. this->setColumnCount(7);
    7. this->setRowCount(2);
    8. date = QDate::currentDate();
    9. this->verticalHeader()->hide();
    10. this->horizontalHeader()->hide();
    11. this->setSpan(0, 1, 1, 5);
    12.  
    13.  
    14. str = "<";
    15. tableItem = createItem(str);
    16. this->setItem(0, 0, tableItem);
    17.  
    18.  
    19. str = ">";
    20. tableItem = createItem(str);
    21. this->setItem(0, 6, tableItem);
    22.  
    23.  
    24.  
    25. /*
    26.   QWidget *asdsa = new WeekCalendarWidgetItem("0", "0");
    27.   this->setCellWidget(0, 1, asdsa);
    28.   */
    29.  
    30.  
    31. connect(this, SIGNAL(cellClicked(int,int)), this, SLOT(changeWeek(int, int)));
    32.  
    33. this->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
    34. countForCalendar();
    35. this->setRowHeight(1, 50);
    36. this->setSelectionMode(QAbstractItemView::SingleSelection);
    37. }
    38.  
    39.  
    40.  
    41.  
    42.  
    43. void WeekCalendarWidget::changeWeek(int row, int column)
    44. {
    45. if (row==0 && column==0)
    46. {
    47. date = date.addDays(-7);
    48. countForCalendar();
    49. }
    50.  
    51. else if (row==0 && column==6)
    52. {
    53. date = date.addDays(7);
    54. countForCalendar();
    55. }
    56. }
    57.  
    58. void WeekCalendarWidget::countForCalendar()
    59. {
    60. int weekNumber = date.weekNumber();
    61. str = "Year " + QString::number(date.year()) + " Week " + QString::number(weekNumber);
    62. tableItem = createItem(str);
    63. this->setItem(0, 1, tableItem);
    64. str.setNum(date.day());
    65.  
    66. tableItem = createItem(str);
    67. this->setItem(1, date.dayOfWeek()-1, tableItem);
    68.  
    69.  
    70.  
    71. QDate date2 = date;
    72. int i = date2.dayOfWeek();
    73.  
    74. while (i>1)
    75. {
    76. date2 = date2.addDays(-1);
    77. str.setNum(date2.day());
    78. tableItem = createItem(str);
    79. this->setItem(1, date2.dayOfWeek()-1, tableItem);
    80. i = date2.dayOfWeek();
    81. }
    82.  
    83. date2 = date;
    84. i = date2.dayOfWeek();
    85. while (i<7)
    86. {
    87. date2 = date2.addDays(1);
    88. str.setNum(date2.day());
    89. tableItem = createItem(str);
    90. this->setItem(1, date2.dayOfWeek()-1, tableItem);
    91. i = date2.dayOfWeek();
    92. }
    93. }
    94.  
    95. QTableWidgetItem * WeekCalendarWidget::createItem(QString string)
    96. {
    97. QTableWidgetItem *tableItem2 = new QTableWidgetItem(string);
    98. tableItem2->setTextAlignment(Qt::AlignCenter);
    99. tableItem2->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
    100. return tableItem2;
    101. }
    102.  
    103. WeekCalendarWidget::~WeekCalendarWidget()
    104. {
    105.  
    106. }
    107.  
    108. void WeekCalendarWidget::test(QWidgetList *lista)
    109. {
    110. asd = lista;
    111. }
    To copy to clipboard, switch view to plain text mode 

    WeekCalendarWidgetItem.cpp:
    Qt Code:
    1. #include "weekcalendarwidgetitem.h"
    2. #include <QtGui>
    3.  
    4. WeekCalendarWidgetItem::WeekCalendarWidgetItem(QString date_par, QString number_par, QWidget *parent)
    5. : QWidget(parent)
    6. {
    7. date = date_par;
    8. number = number_par;
    9.  
    10.  
    11. }
    12.  
    13. void WeekCalendarWidgetItem::paintEvent(QPaintEvent *event)
    14. {
    15.  
    16. painter = new QPainter(this);
    17.  
    18. painter->drawText(this->geometry(), Qt::AlignTop, date);
    19. painter->drawLine(this->geometry().topRight(), this->geometry().bottomLeft());
    20. painter->drawText(this->geometry(), Qt::AlignBottom|Qt::AlignRight, number);
    21.  
    22.  
    23. }
    24.  
    25. WeekCalendarWidgetItem::~WeekCalendarWidgetItem()
    26. {
    27.  
    28. }
    To copy to clipboard, switch view to plain text mode 

    Normal View, where I was using only default setItem function:
    Calendar_Normal_View.jpg

    View, where I set custom Widget to the 0-0 cell:
    Calendar_0-0_cell.jpg

    View, where I set custom Widget to the 0-1 cell:
    Calendar_0-1_cell.jpg

    View, where I set custom Widget to the 1-0 cell:
    Calendar_1-0_cell.jpg

    If anyone has any idea Why this is happening, I would really like to hear it!

  2. The following user says thank you to Archa4 for this useful post:

    louboutinoutlet (2nd December 2013)

  3. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: setCellWidget - strange behavior

    Please provide a minimal compilable example reproducing the problem.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. The following user says thank you to wysota for this useful post:

    louboutinoutlet (2nd December 2013)

  5. #3
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: setCellWidget - strange behavior

    I think this should do it
    Example.zip

    It's very interesting, that I thought the problem would be to resize the custom widget to the size of the cell, but It seems like custom Widget resizes to the cell size, but it just isn't placed right. It shifts to the right multi played by the column number it should be inserted in, and it shifts to the bottom multi played by the row number it should be inserted in...

    Another thing, when i close application I get this line:
    QPaintDevice: Cannot destroy paint device that is being painted
    If while the app was working I selected the cell containing the widget...
    Last edited by Archa4; 27th April 2011 at 10:18.

  6. The following user says thank you to Archa4 for this useful post:

    louboutinoutlet (2nd December 2013)

  7. #4
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: setCellWidget - strange behavior

    Still no one?

  8. The following user says thank you to Archa4 for this useful post:

    louboutinoutlet (2nd December 2013)

  9. #5
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: setCellWidget - strange behavior

    What's the point in using QWidget::geometry() in the paintEvent ? What happens here is that you're drawing your graphics in the bounding rectangle which is the offset that an item has within the tableWidget , while using a coordinate system of the item itself.So kind of double offset takes place here.

    You paintEvent should look like this instead:
    Qt Code:
    1. void WeekCalendarWidgetItem::paintEvent(QPaintEvent *event)
    2. {
    3. QPainter painter(this);
    4.  
    5. QRect bound = event -> rect(); // bounding box of an item to be painted
    6.  
    7. painter.save();
    8.  
    9. // example
    10. painter.drawLine( bound.bottomLeft() , bound.topRight() );
    11. //
    12.  
    13. painter.restore();
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 

    QPaintDevice: Cannot destroy paint device that is being painted
    Ouch, be carefull with the QPainter instances you're creating : every time your item will have to be repainted,you've got a new QPainter instance allocated dynamically : it will not cause a memory leak since it's parented BUT each instance will be kept in memory,unless parent is destroyed or instance is deleted manually. So you can either create QPainter object locally or use a QScopedPointer.
    Last edited by AlexSudnik; 27th April 2011 at 18:10.

  10. The following 2 users say thank you to AlexSudnik for this useful post:

    Archa4 (28th April 2011), louboutinoutlet (2nd December 2013)

  11. #6
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: setCellWidget - strange behavior

    Very Big THANKS!

  12. The following user says thank you to Archa4 for this useful post:

    louboutinoutlet (2nd December 2013)

Similar Threads

  1. QSqlDatabase strange behavior
    By macav in forum Qt Programming
    Replies: 1
    Last Post: 9th February 2011, 15:21
  2. strange behavior of paintEvent
    By hashb in forum Qt Programming
    Replies: 3
    Last Post: 30th August 2010, 07:48
  3. QAudioInput example strange behavior
    By m15ch4 in forum Qt Programming
    Replies: 0
    Last Post: 13th August 2010, 06:55
  4. Strange behavior of QSqlTableModel
    By venomj in forum Qt Programming
    Replies: 0
    Last Post: 13th January 2010, 03:29
  5. scrollbars strange behavior
    By siniy in forum Qt Programming
    Replies: 6
    Last Post: 29th December 2006, 10:27

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.