Results 1 to 9 of 9

Thread: QTableWidget - Adding Checkboxes into Cell/ Deleting Line Edit

  1. #1
    Join Date
    Sep 2009
    Location
    London
    Posts
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default QTableWidget - Adding Checkboxes into Cell/ Deleting Line Edit

    Hi Guys,-
    Its pretty simple to add a checbox into a table widget, but the problem is once adding, the checkbox seems to live on the cell side by side with a QlineEdit(which i dont need).
    I can actually disable the QLineEdit, but im trying to aling the checkboxes to be centered, and it will simply not change its position because it still "sees" the lineedit beside it.

    IIs there a way to properly remove the LineEdit from a cell on QTableWidget?

    appreciate any help,
    thanks!

  2. #2
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: QTableWidget - Adding Checkboxes into Cell/ Deleting Line Edit

    Just a question, why do you want to remove the QLineEdit?

  3. #3
    Join Date
    Sep 2009
    Location
    London
    Posts
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QTableWidget - Adding Checkboxes into Cell/ Deleting Line Edit

    Jano,
    thanks for your quick reply.

    for 2 reasons:
    1. because for that cell in specific, i just need a checkbox and no text input.

    2. And i would like the checkbox to nicely align to the center of the cell (right now, because of the invisible lineedit it will simply be stuck on the left side)
    Attached Images Attached Images

  4. #4
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: QTableWidget - Adding Checkboxes into Cell/ Deleting Line Edit

    one idea... maybe setting the text as "" could do the trick.

  5. #5
    Join Date
    Sep 2009
    Location
    London
    Posts
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QTableWidget - Adding Checkboxes into Cell/ Deleting Line Edit

    cheers for that -
    but being a complete newbie, let me just risk asking a dumb question: is this do-able within Qt Designer?

    (i 'm a graphic designer and mostly using Designer or occasionaly QtCreator)

    thanks,

  6. #6
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: QTableWidget - Adding Checkboxes into Cell/ Deleting Line Edit

    I don't know if there are any other ways, but you can subclass QTableWidgetItem and create your own Items; for instance, one with the checkbox and no text at all.

    But, in my opinion, if you can "cheat" a little bit (like setting and empty string) and solve your problems that way.. better :P So far I didn't need to subclass QTableWidgetItem.

  7. #7

    Default Re: QTableWidget - Adding Checkboxes into Cell/ Deleting Line Edit

    Hi there, I'm a beginner...
    I'm searching for how to add a button into a table, in a cell, then I found your post....
    Could you tell me how you add a checkbox as a QTableWidgetItem ??

    Thanks....

  8. #8

    Default Re: QTableWidget - Adding Checkboxes into Cell/ Deleting Line Edit

    Quote Originally Posted by b_ginner View Post
    Hi there, I'm a beginner...
    I'm searching for how to add a button into a table, in a cell, then I found your post....
    Could you tell me how you add a checkbox as a QTableWidgetItem ??

    Thanks....
    Nevermind, found that already

  9. #9
    Join Date
    Dec 2008
    Posts
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTableWidget - Adding Checkboxes into Cell/ Deleting Line Edit

    Hi Erik,

    I don't know if it is the best solution, but I've found a solution!
    I have subclassed QItemDelegate and only overwrite the paint()-method.
    The implementation of the paint()-method I've "borrowed" from QItemDelegate::paint().

    I only commented out a few lines which refer to QItemDelegatePrivate-class and added a rectangle movement to center the checkbox.

    class CheckBoxDelegate : public QItemDelegate
    {
    Q_OBJECT

    public:
    CheckBoxDelegate( QObject* pParent = 0 ) : QItemDelegate( pParent ) {}
    virtual ~CheckBoxDelegate() {}

    void paint( QPainter *painter,
    const QStyleOptionViewItem &option,
    const QModelIndex &index ) const;
    };

    void CheckBoxDelegate::paint(QPainter *painter,
    const QStyleOptionViewItem &option,
    const QModelIndex &index) const
    {
    //Q_D(const QItemDelegate);
    Q_ASSERT(index.isValid());

    QStyleOptionViewItemV4 opt = setOptions(index, option);

    const QStyleOptionViewItemV2 *v2 = qstyleoption_cast<const QStyleOptionViewItemV2 * >(&option);
    opt.features = v2 ? v2->features
    : QStyleOptionViewItemV2::ViewItemFeatures(QStyleOpt ionViewItemV2::None);
    const QStyleOptionViewItemV3 *v3 = qstyleoption_cast<const QStyleOptionViewItemV3 * >(&option);
    opt.locale = v3 ? v3->locale : QLocale();
    opt.widget = v3 ? v3->widget : 0;

    // prepare
    painter->save();
    if( hasClipping() )
    {
    painter->setClipRect( opt.rect );
    }

    // get the data and the rectangles

    QVariant value;

    QPixmap pixmap;
    QRect decorationRect;
    // IN THIS SPECIAL CASE: no decoration (icon) required -> commented dispayRect calculation
    //value = index.data(Qt::DecorationRole);
    //if (value.isValid()) {
    // // ### we need the pixmap to call the virtual function
    // pixmap = decoration(opt, value);
    // if (value.type() == QVariant::Icon) {
    // d->tmp.icon = qvariant_cast<QIcon>(value);
    // d->tmp.mode = d->iconMode(option.state);
    // d->tmp.state = d->iconState(option.state);
    // const QSize size = d->tmp.icon.actualSize(option.decorationSize,
    // d->tmp.mode, d->tmp.state);
    // decorationRect = QRect(QPoint(0, 0), size);
    // } else {
    // d->tmp.icon = QIcon();
    // decorationRect = QRect(QPoint(0, 0), pixmap.size());
    // }
    //} else {
    // d->tmp.icon = QIcon();
    // decorationRect = QRect();
    //}

    QString text;
    QRect displayRect;
    // IN THIS SPECIAL CASE: no text required -> commented dispayRect calculation
    //value = index.data(Qt::DisplayRole);
    //if (value.isValid() && !value.isNull()) {
    // text = QItemDelegatePrivate::valueToText(value, opt);
    // displayRect = textRectangle(painter, textLayoutBounds(opt), opt.font, text);
    //}

    QRect checkRect;
    Qt::CheckState checkState = Qt::Unchecked;
    value = index.data(Qt::CheckStateRole);
    if (value.isValid()) {
    checkState = static_cast<Qt::CheckState>(value.toInt());
    checkRect = check(opt, opt.rect, value);
    }

    // do the layout

    doLayout(opt, &checkRect, &decorationRect, &displayRect, false);

    // move checkBox to the center of the column
    checkRect.moveRight( ( opt.rect.width() + checkRect.width() ) / 2.0 );

    // draw the item

    drawBackground(painter, opt, index);
    drawCheck(painter, opt, checkRect, checkState);

    // no decoration, no text and no focus -> no need to draw!!!
    //drawDecoration(painter, opt, decorationRect, pixmap);
    //drawDisplay(painter, opt, displayRect, text);
    //drawFocus(painter, opt, displayRect);

    // done
    painter->restore();
    }

    To set CheckBoxDelegate() for the first column of the tableWidget,
    you only need to add the following line:

    tableWidget->setDelegateItemForColumn( 0, new CheckBoxDelegate() );

    Good luck!

Similar Threads

  1. QTcpSocket exception.
    By Fastman in forum Qt Programming
    Replies: 9
    Last Post: 29th January 2008, 13:51
  2. Adding a line edit in a toolbar
    By borges in forum Qt Tools
    Replies: 1
    Last Post: 16th August 2007, 15:33
  3. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  4. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42

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.