Results 1 to 13 of 13

Thread: tablewidget text colour

  1. #1
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default tablewidget text colour

    I'm trying to set the text colour for selected text in a spreadsheet (tablewidget). I've tried several different methods but none of them compile. First I wrote this code for changing the font. This works fine.

    both functions on spreadsheet.cpp
    Qt Code:
    1. void Spreadsheet::selectCellFont()
    2. {
    3. QList<QTableWidgetItem *> items = selectedItems();
    4. if (items.isEmpty())
    5. return; // nothing happens if nothing is selected and the dialog doesn't appear
    6.  
    7. bool ok = false;
    8. QFont fnt = QFontDialog::getFont(&ok, font(), this);
    9.  
    10. if (!ok) // if set to OK nothing is changed
    11. return;
    12. foreach (QTableWidgetItem *item, items)
    13. if(item)
    14. item->setFont(fnt);
    15. // somethingChanged();
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 

    I then wrote similar code for text colour

    Qt Code:
    1. void SpreadSheet::selectFontColour()
    2. {
    3. QList<QTableWidgetItem *> items = selectedItems();
    4. if (items.isEmpty())
    5. return; // nothing happens if nothing is selected and the dialog doesn't appear
    6. bool ok = false;
    7.  
    8. QColor col = QColorDialog::getColor(&ok, items->textColor(), this);
    9. if (!ok)
    10. return;
    11. foreach (QTableWidgetItem *item, items)
    12. if (item)
    13. item->setTextColor(col);
    14. }
    To copy to clipboard, switch view to plain text mode 

    with prototypes in Spreadsheet class on spreadsheet.hpp

    Qt Code:
    1. class Spreadsheet : public QTableWidget
    2.  
    3. {
    4. Q_OBJECT
    5.  
    6. public://
    7.  
    8. Spreadsheet(const QString &fileName = QString(),
    9. QWidget *parent = 0);
    10. QString currentLocation() const;
    11. QString currentFormula() const;
    12.  
    13. void clear();
    14. QTableWidgetSelectionRange selectedRange() const;
    15. bool load(const QString &fileName = QString());
    16. bool save(const QString &fileName = QString());
    17. void printHtml(const QString &html);
    18.  
    19.  
    20. public slots://
    21.  
    22. void selectCellFont();
    23. void selectFontColour();
    24. // etc
    To copy to clipboard, switch view to plain text mode 

    I get these errors


    src/spreadsheet.cpp:312: error: ‘SpreadSheet’ has not been declared
    src/spreadsheet.cpp: In function ‘void selectFontColour()’:
    src/spreadsheet.cpp:314: error: ‘selectedItems’ was not declared in this scope
    src/spreadsheet.cpp:319: error: base operand of ‘->’ has non-pointer type ‘QList<QTableWidgetItem*>’
    src/spreadsheet.cpp:319: error: invalid use of ‘this’ in non-member function

    The first one is the most puzzling since the font function works.... Can anyone help?

  2. #2
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: tablewidget text colour

    Try "QTextEdit{ selection-background-color: red }" .

  3. #3
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: tablewidget text colour

    Thanks for your reply but I'm using Qtablewidget not Qtextwidget

    I put this line in the clear function which makes the header text blue
    Qt Code:
    1. item->setTextColor ( Qt::blue );
    To copy to clipboard, switch view to plain text mode 

    but I want to make it interactive.

  4. #4
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: tablewidget text colour

    Sorry for that little mess

    Just replace QTextEdit with QTableView and you better do this from the designer..

    By the way is your table is multi selected?

  5. #5
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: tablewidget text colour

    Thanks that's OK but I'm not sure tableview is what I want. I want to alter the format of the text?

    altering the font was easy using tablewidget but changing its colour is more difficult at least for me.....

  6. #6
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: tablewidget text colour

    Oh you want to change the color of the text not background of it?

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: tablewidget text colour

    Note the difference between Spreadsheet and SpreadSheet. C++ is case sensitive!

    And then better use setForeground since setTextColor is obsolete.

  8. #8
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: tablewidget text colour

    Plus I guess you need to set background color as well since it's default dark blue or something. But I still suggest you to stick with selection-color and selection-background-color.

  9. #9
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: tablewidget text colour

    What an idiot! of course it is I must have misstyped it.... It just shows you need someone to look over your code... The only error now is

    Qt Code:
    1. QColor col = QColorDialog::getColor(&ok, items->textColor(), this);
    To copy to clipboard, switch view to plain text mode 

    Is still cannot work out to do for this line though even after trying a number of alternatives.



    I've tried looking at the documentation but I don't find it helpful, the fontdialog documentation is much better it gives a simple code example which helps.

    I assume I need something like

    Qt Code:
    1. QColor col = QColorDialog::getColor( items = Qt::black, items);
    2. QColor col = QColorDialog::getColor(&ok, items = Qt::black, items); ??
    To copy to clipboard, switch view to plain text mode 

    then
    Qt Code:
    1. if (!ok)
    2. return;
    3. foreach (QTableWidgetItem *item, items)
    4. if (item)
    5. item->setForeground(col);
    To copy to clipboard, switch view to plain text mode 

    Any help appreciated.

  10. #10
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: tablewidget text colour

    Simply use
    Qt Code:
    1. QColor col = QColorDialog::getColor(items->at(0)->textColor(), this);
    To copy to clipboard, switch view to plain text mode 
    and instead of checking your bool ok use
    Qt Code:
    1. cal ==items->at(0)->textColor()
    To copy to clipboard, switch view to plain text mode 

    And once again the same advice: don't use obsolete members. Use foreground() instead. And see the docs how functions are used, since getting a color is a little bit different from getting a font.

  11. #11
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: tablewidget text colour

    Thanks Lykurg and everyone I'm embarrassed to say I've solved it. After straining over the documentation and finding a snippet of code in the C++ GUI programming with QT4 book. I couldn't work out how to set the
    Qt Code:
    1. const QColor & initial = Qt::colour
    To copy to clipboard, switch view to plain text mode 

    but I worked from the book I needed

    Qt Code:
    1. private:
    2. QColor colour; // where colour is initial
    To copy to clipboard, switch view to plain text mode 
    and "this" as the functions second arguement then it works fine

    Code for anyone else who might need it, setBackground() does the same thing.

    Qt Code:
    1. void Spreadsheet::selectFontColour()
    2. {
    3. QList<QTableWidgetItem *> items = selectedItems();
    4. if (items.isEmpty())
    5. return; // nothing happens if nothing is selected and the dialog doesn't appear
    6. QColor col = QColorDialog::getColor( colour = Qt::black, this); // set to black since cell background is white just in case
    7. foreach (QTableWidgetItem *item, items) // if cancel is pressed no change
    8. if (item)
    9. item->setForeground(col);
    10. }
    To copy to clipboard, switch view to plain text mode 

  12. #12
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: tablewidget text colour

    I am sorry to say, but your code did not look nice... You don't have to create a member variable for that. This is a overkill. Just pass a color you like as default. That's it. (And the validation in line 8 is not necessary.) And check if the returned color is valid.

  13. #13
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: tablewidget text colour

    Thanks. I'll give it a go.

Similar Threads

  1. How to change Label text colour in Qt designer
    By sudhansu in forum Qt Programming
    Replies: 2
    Last Post: 6th January 2010, 12:31
  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. How to change Text colour in EditText?
    By phillip_Qt in forum Qt Programming
    Replies: 6
    Last Post: 6th March 2008, 08:48
  5. background colour
    By kw in forum Qt Programming
    Replies: 6
    Last Post: 11th April 2006, 00:44

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.