Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. SpinBoxDelegate example.
Qt 6.11 is out! See what's new in the release blog

SpinBoxDelegate example.

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 600 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    leeroy
    wrote on last edited by
    #1

    Hi,

    I was reviewing the following SpinBoxDelegate example code:
    https://doc.qt.io/qt-5/qtwidgets-itemviews-spinboxdelegate-example.html

    Wanted to know if there is a way to centre align the value on the spinbox's? I tried a few variations including the following but neither is working.

    QWidget *SpinBoxDelegate::createEditor(QWidget parent,
    const QStyleOptionViewItem &/
    option /,
    const QModelIndex &/
    index */) const
    {
    QSpinBox *editor = new QSpinBox(parent);
    editor->setFrame(false);
    editor->setMinimum(0);
    editor->setMaximum(100);
    editor->setAlignment(Qt::AlignHCenter);

    return editor;
    

    }

    and...

    void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    const QModelIndex &index) const
    {
    QSpinBox spinBox = static_cast<QSpinBox>(editor);
    spinBox->interpretText();
    int value = spinBox->value();

    model->setData(index, value, Qt::EditRole);
    **model->setData(index, Qt::AlignHCenter, Qt::TextAlignmentRole);**
    

    }

    Thanks.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Do you want to center the content of the cell or the content of the spin box ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi and welcome to devnet,

        Do you want to center the content of the cell or the content of the spin box ?

        L Offline
        L Offline
        leeroy
        wrote on last edited by
        #3

        Hi @SGaist I would like to centre the content of the spinbox.

        1 Reply Last reply
        0
        • L Offline
          L Offline
          leeroy
          wrote on last edited by
          #4

          Hi @SGaist any thoughts how the content of the spinbox can be centred? Thanks in advance.

          mrjjM 1 Reply Last reply
          0
          • L leeroy

            Hi @SGaist any thoughts how the content of the spinbox can be centred? Thanks in advance.

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @leeroy
            Hi
            It has align.
            alt text

            alt text

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved