Results 1 to 8 of 8

Thread: TextAlignment / no RowCount in QStandardItemModel

  1. #1
    Join Date
    Jun 2008
    Posts
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default TextAlignment / no RowCount in QStandardItemModel

    I set up an QStandardItemModel an show it in an QTableView.
    When inserting several rows, first colum shows the row number.
    How to disable them?

    Also what the second colum (first row with my content) shows the text right alligned.
    And i want the colum width set to minimum, regarding content text width.

    Haven´t found any hints - can you help me?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: TextAlignment / no RowCount in QStandardItemModel

    The first "column" is a header - you can hide it. Alignment is set using Qt::TextAlignmentRole on each item. As for "width set to minimum" - define "minimum" You can change the resize policy for the horizontal header if you want, maybe it will be sufficient in your case.

  3. The following user says thank you to wysota for this useful post:

    borzh62 (31st October 2015)

  4. #3
    Join Date
    Jun 2008
    Posts
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: TextAlignment / no RowCount in QStandardItemModel

    Quote Originally Posted by wysota View Post
    The first "column" is a header - you can hide it.
    Sorry, but did you mean "can" or "can´t"? Because if i can, i wonder again how.


    Quote Originally Posted by wysota View Post
    Qt::TextAlignmentRole on each item.
    I need to display ints and text. This data is gathered from other objects.
    I´ve tried to avoid setting up explicit an QStandardItems for setting it as model data.

    How i did it:
    int:
    model->setData(model->index(row, 0, QModelIndex()), i+1);
    text:
    model->setData(model->index(row, 0, QModelIndex()), QString::fromStdString(object->getTextAt(j) );

    But where to set the Qt::TextAllignmentRole when doing it this way?

    BTW: My exxample is very simple. I didn´t set up special classes for the model or it´s items. Didn´t use subclasing in any way. It´s just one method in my mainwindowclass, where i set up a QStandartmodel, gathering some data to show the model in QTableView.
    I need to keep it simple - also if it´s not confentional or seems to be stupid - there is no time left.

    Next year going to learn QT from the beginning - i swear!

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: TextAlignment / no RowCount in QStandardItemModel

    Quote Originally Posted by SenSej View Post
    Sorry, but did you mean "can" or "can´t"? Because if i can, i wonder again how.
    "Can" By calling.... hide() on it Have you seen QTableView docs? They are really nice, you should at least take a brief look at them...

    I need to display ints and text. This data is gathered from other objects.
    I´ve tried to avoid setting up explicit an QStandardItems for setting it as model data.
    So return appropriate alignment from data() based on the contents of the item. data() is a virtual method, you can reimplement it.

    But where to set the Qt::TextAllignmentRole when doing it this way?
    I take it that you... forgot... to read the docs. See what setData() accepts.

    BTW: My exxample is very simple. I didn´t set up special classes for the model or it´s items. Didn´t use subclasing in any way. It´s just one method in my mainwindowclass, where i set up a QStandartmodel, gathering some data to show the model in QTableView.
    I need to keep it simple - also if it´s not confentional or seems to be stupid - there is no time left.
    You don't need to subclass anything.

    Next year going to learn QT from the beginning - i swear!
    Ok, but this year start by reading documentation of methods and classes you are using.

  6. #5
    Join Date
    Jun 2008
    Posts
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: TextAlignment / no RowCount in QStandardItemModel

    First of all - Thanks a lot for you help.
    Hiding wasn´t a matter of the model, but the view. I didn´t set up any view, so i didn´t recognize this. I´ve hided the vertical Header of the QTableView and now all´s fine.

    By using
    Qt Code:
    1. int intVal = 89;
    2. QStandardItem* itemInt = new QStandardItem();
    3. itemInt->setData(intVal, Qt::AlignRight);
    4. model->setData(model->index(row, 0, QModelIndex()), itemInt);
    To copy to clipboard, switch view to plain text mode 
    "true" will be displayed and not 89.

    Just wanted to thank you and start now to read. But any hints are stil wellcome.

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: TextAlignment / no RowCount in QStandardItemModel

    Quote Originally Posted by SenSej View Post
    By using
    Qt Code:
    1. int intVal = 89;
    2. QStandardItem* itemInt = new QStandardItem();
    3. itemInt->setData(intVal, Qt::AlignRight);
    4. model->setData(model->index(row, 0, QModelIndex()), itemInt);
    To copy to clipboard, switch view to plain text mode 
    "true" will be displayed and not 89.
    setData takes a value and a role. A role is something that tells the model, what you want to do with the value you give it. If you want the value to be displayed, you pass Qt:isplayRole, if you want it to determine what font is used, you pass it Qt::FontRole. If you want to tell what the text alignment should be, you pass it a Qt::TextAlignmentRole. So the proper sequence of calls in your case is:
    Qt Code:
    1. setData(89, Qt::DisplayRole); // display 89
    2. setData(Qt::AlignRight, Qt::TextAlignmentRole); // aligned to the right
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    Jun 2008
    Posts
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: TextAlignment / no RowCount in QStandardItemModel

    Quote Originally Posted by wysota View Post
    Qt Code:
    1. setData(89, Qt::DisplayRole); // display 89
    2. setData(Qt::AlignRight, Qt::TextAlignmentRole); // aligned to the right
    To copy to clipboard, switch view to plain text mode 
    Thanks - Now i got you right!
    But in my doku, the signature of "setData" come with QModelIndex as first param.
    When using also the index it works fine.

    Pherhaps you´ve got one more hint for me:
    My QStandardItemModel got three colums.
    The first and second are only to display some items.
    I want to use the third colum as input fields.
    So i need to disable any editor-action for the first and second, the third schould be marked and differ in graphic from the rest, with enabled editor functionality.

    In the doku i´ve found the role-enum, but not which values are suitable for each role.

    Is it possible, to set another widget as an item to one field in the model (like an SpinBox or LineEdit)?

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: TextAlignment / no RowCount in QStandardItemModel

    Quote Originally Posted by SenSej View Post
    Thanks - Now i got you right!
    But in my doku, the signature of "setData" come with QModelIndex as first param.
    When using also the index it works fine.
    If you call the model directly, then index comes as the first parameter. If you call setData on QStandardItem, it takes only two parameters.

    Pherhaps you´ve got one more hint for me:
    My QStandardItemModel got three colums.
    The first and second are only to display some items.
    I want to use the third colum as input fields.
    So i need to disable any editor-action for the first and second, the third schould be marked and differ in graphic from the rest, with enabled editor functionality.
    Take a look at QAbstractItemModel::flags() and QStandardItem::flags().

    Is it possible, to set another widget as an item to one field in the model (like an SpinBox or LineEdit)?
    Yes, but you don't want to do that.

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.