Results 1 to 5 of 5

Thread: How to properly change font weight in model?

  1. #1
    Join Date
    Oct 2013
    Posts
    142
    Thanks
    36
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default How to properly change font weight in model?

    I am having issues setting font weight for a column inside QTableView's model.
    I found that I can use QFont for it, but I cannot find a way to set only the font weight, I want the text bolded for some cells.
    I tried it like this:

    Qt Code:
    1. QVariant ModelLIes::data(const QModelIndex &index, int role) const{
    2. QVariant value = QSqlQueryModel::data(index, role);
    3. if(index.column() == 5){
    4. if(role == Qt::FontRole){
    5. int valStare=QSqlQueryModel::data(index, Qt::DisplayRole).toInt(); // to check which cells to modify in the column
    6. QFontInfo info(QSqlQueryModel::data(index, Qt::DisplayRole).toString());
    7. int marime=info.pointSize(); //tried also pixelSize();
    8. QString fam=info.family();
    9. if(valStare==1)
    10. return QFont(fam, marime,QFont::Bold);
    11. }
    12. }
    13. return value;
    14. }
    To copy to clipboard, switch view to plain text mode 

    But the font is different from the rest of my tableview.(Is it setting the font size and family to what I am using on my OS?)

    Any suggestions?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to properly change font weight in model?

    You are getting font info from the database but you are not applying to to all columns.
    If you always want to use the font specified in the database then you have to answer FontRole for all cells. You can still change it for the cells you want bold of course.

    Btw, are you sure that the value returned by the base class is both a number and a font family? Usually font families are not just numbers.

    Cheers,
    _

  3. #3
    Join Date
    Oct 2013
    Posts
    142
    Thanks
    36
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to properly change font weight in model?

    Oh, I misunderstood the usage, I was trying to get the font from the tableview, but it takes the font from database.

    Though I do not understand:
    Btw, are you sure that the value returned by the base class is both a number and a font family? Usually font families are not just numbers.
    I use:
    Qt Code:
    1. int valStare=QSqlQueryModel::data(index, Qt::DisplayRole).toInt();
    To copy to clipboard, switch view to plain text mode 
    To check the value returned by the query which is 0 or 1 so it knows wether to change the font or not.

    Qt Code:
    1. QFontInfo info(QSqlQueryModel::data(index, Qt::DisplayRole).toString());
    To copy to clipboard, switch view to plain text mode 
    is used to get font info like font family and size, though now I understood that it took the fony from the database and not the application itself(more exactly the QTableView).

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to properly change font weight in model?

    If you don't have font information in the database, it might be easier to do the visual differentiation with an item delegate for the respective column.

    Cheers,
    _

  5. #5
    Join Date
    Oct 2013
    Posts
    142
    Thanks
    36
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to properly change font weight in model?

    ok thank you

Similar Threads

  1. [Qt5]How to change the Font in Linguist?
    By newkedison in forum Qt Programming
    Replies: 1
    Last Post: 31st May 2013, 07:41
  2. Replies: 0
    Last Post: 24th October 2011, 07:43
  3. QWebView does not display Specific font properly ?
    By mismael85 in forum Qt Programming
    Replies: 4
    Last Post: 8th February 2011, 16:05
  4. Style sheets and font weight bug
    By Oleg in forum Qt Programming
    Replies: 8
    Last Post: 14th February 2010, 22:19
  5. Change Font of QListWidget to Monospace Font
    By pospiech in forum Qt Programming
    Replies: 3
    Last Post: 25th July 2008, 18:23

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.