Results 1 to 6 of 6

Thread: setCellWidget - strange behavior

Threaded View

Previous Post Previous Post   Next Post Next Post
  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:


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