Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8321873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:10:31+00:00 2026-06-08T23:10:31+00:00

I have created one table by using QTableview and QAbstractTableModel . In one of

  • 0

I have created one table by using QTableview and QAbstractTableModel .
In one of the cells, I want to add one help button in the right corner of that cell.

Is there any way to achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T23:10:34+00:00Added an answer on June 8, 2026 at 11:10 pm

    You will have to implement your own delegate for that.

    In Qt, aside from the Data, the Model and the View, you have your Delegates. They provide input capabilities, and they are also responsible for rendering “special” items in the View, which is what you need.

    Qt doc has a good coverage on those (keywords: Model/View programming), and you can also find some examples here and here.

    Also (a little off-topic, but I think I should point this out), if you use an ordinary QTableWidget, you can insert anything into any cell with it’s setCellWidget() function.

    UPD

    here is a slightly modified example from Qt docs (I suck with model/view stuff in Qt so don’t beat me hard for this code). It will draw a button in each cell on the right, and catch the click events in cells to check if the click was on the “button”, and react accordingly.

    Probably this is not the best way to do it, but as I mentioned, I’m not too good with Qt’s models and views.

    To do things right and allow proper editing, you will need to also implement createEditor(), setEditorData() and setModelData() functions.

    To draw your stuff in a specific cell instead of all cells, just add a condition into the paint() function (note that it gets the model index as an argument, so you can always know in what cell you are painting, and paint accordingly).


    delegate.h:

    class MyDelegate : public QItemDelegate
    {
        Q_OBJECT
    
    public:
        MyDelegate(QObject *parent = 0);
        void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
        bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
    };
    

    delegate.cpp:

     #include <QtGui>
     #include "delegate.h"
    
     MyDelegate::MyDelegate(QObject *parent)
         : QItemDelegate(parent)
     {
     }
    
    
     void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
     {
         QStyleOptionButton button;
         QRect r = option.rect;//getting the rect of the cell
         int x,y,w,h;
         x = r.left() + r.width() - 30;//the X coordinate
         y = r.top();//the Y coordinate
         w = 30;//button width
         h = 30;//button height
         button.rect = QRect(x,y,w,h);
         button.text = "=^.^=";
         button.state = QStyle::State_Enabled;
    
         QApplication::style()->drawControl( QStyle::CE_PushButton, &button, painter);
     }
    
     bool MyDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
     {
         if( event->type() == QEvent::MouseButtonRelease )
         {
             QMouseEvent * e = (QMouseEvent *)event;
             int clickX = e->x();
             int clickY = e->y();
    
             QRect r = option.rect;//getting the rect of the cell
             int x,y,w,h;
             x = r.left() + r.width() - 30;//the X coordinate
             y = r.top();//the Y coordinate
             w = 30;//button width
             h = 30;//button height
    
             if( clickX > x && clickX < x + w )
                 if( clickY > y && clickY < y + h )
                 {
                     QDialog * d = new QDialog();
                     d->setGeometry(0,0,100,100);
                     d->show();
                 }
         }
    
         return true;
     }
    

    main.cpp

    #include "delegate.h"
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        QStandardItemModel model(4, 2);
        QTableView tableView;
        tableView.setModel(&model);
    
        MyDelegate delegate;
        tableView.setItemDelegate(&delegate);
    
        tableView.horizontalHeader()->setStretchLastSection(true);
        tableView.show();
        return app.exec();
    }
    

    The result will look like this:

    result

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created one table using the below command: create table Table1( Id int
I have created one jTable. I want to display the data into table from
I have a table that has no primary key, and one cannot be created
I have a view controller with table view that contains 8 cells(sections).I have created
I have to create one table using XSLT and CSS. The table should look
i have one question about jquery ajax().. I created a table grid and there
I have a huge table from one mysql db, I want to create a
I have created one service in my grails application. in that service 25 methods
I have created one function whose purpose is to add data to database (I'm
I 'm new to the asp.net. I have created one web page in that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.