Results 1 to 19 of 19

Thread: QTimer

  1. #1
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QTimer

    Hello anyone,

    Iám using qt-4.4.1 with Windows XP.
    I have subclassed QSqLTableModel into CustomSqlModel for the BackgroundRole for setting different colors in columns and Textalignment.
    In the main implentation i use QTableview as model.
    Everything works perfect.
    I have also subclassed QItemDelegate for setting background color for a column with a certain value. ( see example code below).
    Now my question is i want to blink the cells with a QTimer in the subclassed QItemDelegate.
    Is this possible and can you give me some example code.
    QTimer use Signal and a Slot but the paint constructor is public and not a slot.

    Qt Code:
    1. #include "delegate.h"
    2. #include <QPainter>
    3. #include <QTimer>
    4.  
    5. void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    6. {
    7.  
    8. int val = index.model()->data(index, Qt::DisplayRole).toInt() < 1 ;
    9. if(index.column() == 8 && val )
    10.  
    11. {
    12. painter->fillRect(option.rect, Qt::yellow);
    13.  
    14. }
    15. else
    16.  
    17. QItemDelegate::paint(painter,option,index);
    18. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: QTimer

    Hmm... tough problem. Not because what you want is not possible -- it is quite easy. The real problem is where to implement it. Is blinking a feature of the model? Or maybe the view? Would you want the same cell to blink in all views showing it or only in a particular one? Answer to those questions depends on why you want cells to blink in the first place.

    If you want blinking to be part of the model, then simply provide a custom slot for your model where you will change the background role of the item and emit dataChanged.

    If you want blinking to be associated with a particular view, then you have to change the way the view renders cells - either through the delegate or somehow directly through the view. Remember that the delegate is shared for all items in a view and possibly between different views as well, so storing any information about blinking items in the delegate might not be a good idea. A possible nice solution in this case would be to provide a custom proxy model between your base model and the view so that you can control blinking from the proxy model in the same way as described earlier.

  3. #3
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTimer

    Hello wysota,

    I want blink cells to be part of the model.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: QTimer

    So simply change the background role of items you want to blink using a timer or a timerEvent. If you emit the dataChanged signal, all views will get updated.

  5. #5
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTimer

    Must i do that in the Main implentation or in the subclassed QSqltableModel.

    Thanks in advanced.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: QTimer

    The QSqlTableModel doesn't allow using the background role, so you'll need to provide support for it in the subclass. Your model has to return proper data from the data() method.

  7. #7
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTimer

    wysota thanks for your advice and time.

  8. #8
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTimer

    Iám still struggle to get the cells background to blink in the model with a timer.
    I have a customslot declared in the customsqlmodel.h (see code below).
    Qt Code:
    1. public:
    2. CustomSqlModel(QObject *parent = 0);
    3. QVariant data(const QModelIndex &index, int role) const;
    4.  
    5. public slots:
    6. void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight); // I don't no this is the right slot
    7.  
    8. private:
    9. CustomSqlModel *model;
    To copy to clipboard, switch view to plain text mode 

    In my customsqlmodel.cpp(see code below).
    Qt Code:
    1. CustomSqlModel::CustomSqlModel(QObject *parent)
    2. : QSqlTableModel(parent)
    3. {
    4. QTimer *timer = new QTimer(this);
    5. connect(timer, SIGNAL(timeout()), this, SLOT(dataChanged(const QModelIndex&, const QModelIndex&))); //Can i connect the timer to the dataChanged() slot on this way?
    6. timer->start(1000);
    7.  
    8. //a custom slot
    9. connect(model, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(dataChanged(const QModelIndex &topLeft, QModelIndex &bottomRight)));
    10. }
    11.  
    12. QVariant CustomSqlModel::data(const QModelIndex &index, int role) const
    13. {
    14. QVariant value = QSqlTableModel::data(index, role);
    15. if (role == Qt::BackgroundColorRole)
    16. {
    17. int val = index.model()->data(index, Qt::DisplayRole).toInt() < 1;
    18. if(index.column() == 8 && val )
    19. return qVariantFromValue(QColor(Qt::yellow)); // Want to blink these color with a timer into the custom slot dataChanged().
    20. return qVariantFromValue(QColor(Qt::grey));
    21. }
    22.  
    23. return value;
    24. }
    25.  
    26. void CustomSqlModel::dataChanged(const QModelIndex &topLeft, QModelIndex &bottomRight)
    27. {
    28. // What to put here.
    29. emit dataChanged(topLeft, bottomRight); //?
    30. }
    To copy to clipboard, switch view to plain text mode 


    Can someone give my some example code.
    I tried severall things but nothings works.
    Thanks in advance.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: QTimer

    Signal and slot signatures have to match, that's for sure.

  10. #10
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTimer

    Ok, I will recode my application to get it work.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: QTimer

    I think you are making things overcomplicated. Try something like this:

    Qt Code:
    1. void mymodel::timerEvent(QTimerEvent *te){
    2. blinkon = !blinkon;
    3. int topRow = rowCount();
    4. int bottomRow = -1;
    5. for(int i=0;i<rowCount();i++){
    6. for(int j=0;j<columnCount();j++){
    7. if(data(index(i, j), IsBlinkingRole).toBool()){
    8. if(blinkon)
    9. setData(index(i,j), Qt::red, Qt::BackgroundRole);
    10. else
    11. setData(index(i,j), QVariant(), Qt::BackgroundRole);
    12. if(i<topRow) topRow = i; if(i>bottomRole) bottomRole = i;
    13. }
    14. }
    15. if(bottomRow>=0)
    16. emit dataChanged(index(topRow, 0), index(bottomRow, columnCount()-1));
    17. }
    To copy to clipboard, switch view to plain text mode 
    Now it's enough to use startTimer with the blink period, initialize "blinkon" variable and set IsBlinkingRole on items you want to blink. Of course your model has to support both BackgroundRole and the custom IsBlinkingRole.

  12. #12
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTimer

    Thanks for the example code.
    I,am using qt 4-4-1 (opensource).
    Qt Code:
    1. if(data(index(i, j), [U]IsBlinkingRole[/U]).toBool())
    To copy to clipboard, switch view to plain text mode 
    My CustomSqlModel support BackgroundRole but not IsBlinkingRole.
    Must i create first a custom IsBlinkingRole.

    Thanks in advance

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: QTimer

    Yes, you must implement that role for your model.

  14. #14
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTimer

    I have initialize "blinkon" variable like this:
    Qt Code:
    1. static bool blinkon = true;
    To copy to clipboard, switch view to plain text mode 
    I have made on the top of my customsqlmodel.cpp a customRole IsBlinkingRole like this:
    Qt Code:
    1. #include <QtGui>
    2. #include <QTimerEvent>
    3.  
    4. #include "customsqlmodel.h"
    5.  
    6. const int IsBlinkingRole = Qt::UserRole;
    To copy to clipboard, switch view to plain text mode 
    I have a startTimer in my customsqlmodel.cpp like this:
    Qt Code:
    1. CustomSqlModel::CustomSqlModel(QObject *parent)
    2. : QSqlTableModel(parent)
    3. {
    4. startTimer(1000);
    5. }
    To copy to clipboard, switch view to plain text mode 
    My question is how to set the items that i want to blink to my customRole in my data() method.
    Normally i can use Qt:isplayRole or Qt::BackgroundRole etc.

    Thanks in advance.

  15. #15
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTimer

    Hello anyone,

    Iám still struggle to set the items that i want to blink to my IsBlinkingRole in my data() method.
    Qt Code:
    1. #include <QtGui>
    2. #include <QTimerEvent>
    3. #include "customsqlmodel.h"
    4. const int IsBlinkingRole = Qt::UserRole;
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. if(role ==Qt:: BackgroundRole)
    2. {
    3. If(role == isBlinkingRole)
    4. {
    5. int val = index.model()->data(index, Qt::DisplayRole).toInt() < 1 ;
    6. if(index.column() == 8 && val )
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: QTimer

    The above code makes no sense. If the first if is entered, the second isn't. So the inner if block never gets executed.

  17. #17
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTimer

    Can you give me some example how to set the items to the CustomRole.

    Thanks in advance.

  18. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: QTimer

    If you use a model that doesn't store custom data (like all sql models), you have to provide a container for them in the model and reimplement setData() to store the incoming data in the container.

  19. #19
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTimer

    Thanks for your time and advice.
    I will look further on the net and the forum threads for setData() examples.

Similar Threads

  1. Replies: 8
    Last Post: 27th March 2013, 11:51
  2. Extending QTimer()
    By rishid in forum Qt Programming
    Replies: 3
    Last Post: 7th August 2009, 01:59
  3. QTimer problem
    By rajeshs in forum Qt Programming
    Replies: 3
    Last Post: 13th September 2008, 14:33
  4. QThread/QObject and QTimer
    By swiety in forum Qt Programming
    Replies: 2
    Last Post: 25th January 2008, 08:37
  5. Replies: 5
    Last Post: 6th March 2007, 05:34

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.