Results 1 to 4 of 4

Thread: Error in changing the colour of the item of a listwidget

  1. #1
    Join Date
    Sep 2017
    Posts
    23
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Error in changing the colour of the item of a listwidget

    I am trying to do some operation of the selected item in a list widget if a pushbutton is clicked. this is done using the following code
    Qt Code:
    1. void Dialog::on_pushButton_clicked()
    2. {
    3. if(MyListWidget->currentItem()->isSelected())
    4. {
    5. MyListWidget->currentItem()->setText("YOU");
    6. QString my_text= MyListWidget->currentItem()->text();
    7. qDebug()<<my_text;
    8.  
    9. MyListWidget->currentItem()->setBackgroundColor(Qt::green);
    10. }
    11. else return;
    12. }
    To copy to clipboard, switch view to plain text mode 
    The program works well if we select an item before pressing the pushbutton,the problem occurs if no item is selected before clicking the button, the programme is crashed

  2. #2
    Join Date
    Apr 2014
    Posts
    59
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Error in changing the colour of the item of a listwidget

    What happens if you just remove the else statement?

  3. #3
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    507
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Error in changing the colour of the item of a listwidget

    Hi, just guessing, but I think MyListWidget->currentItem() will return NULL.

    A debugger will help you to determine the position and reason of the crash.

    Ginsengelf

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Error in changing the colour of the item of a listwidget

    Try this:
    Qt Code:
    1. void Dialog::on_pushButton_clicked()
    2. {
    3. QListWidgetItem *pCurrItem = MyListWidget->currentItem();
    4. if(pCurrItem && pCurrItem->isSelected())
    5. {
    6. pCurrItem->setText("YOU");
    7. QString my_text= pCurrItem->text();
    8. qDebug()<<my_text;
    9.  
    10. pCurrItem->setBackgroundColor(Qt::green);
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Colour changing on qpushbuttons through code
    By Anjali123 in forum Newbie
    Replies: 0
    Last Post: 25th November 2016, 05:48
  2. Changing QPushButton text colour with mouseMoveEvent
    By Misenko in forum Qt Programming
    Replies: 1
    Last Post: 10th June 2008, 16:53
  3. Changing text colour of QLineEdit qss file
    By phillip_Qt in forum Qt Programming
    Replies: 2
    Last Post: 4th April 2008, 05:28
  4. Changing colour thru qss file
    By phillip_Qt in forum Qt Programming
    Replies: 3
    Last Post: 27th March 2008, 05:36
  5. Changing the colour of an individual widget
    By georgie in forum Qt Programming
    Replies: 6
    Last Post: 2nd May 2006, 02:53

Tags for this Thread

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.