0

In my application , im having table widget , i want to set the text alignment center for all the cells in the table. For that i tried like,

 QTableWidgetItem * protoitem = new QTableWidgetItem();
 protoitem->setTextAlignment(Qt::AlignRight);
 tableWidget->setItemPrototype(protoitem);

but it not working properly, guide me,

shivcena
  • 2,023
  • 8
  • 24
  • 50
  • possible duplicate of [Set default alignment for cells in QT Table Widget](http://stackoverflow.com/questions/15827886/set-default-alignment-for-cells-in-qt-table-widget) – SingerOfTheFall Sep 28 '15 at 12:16

1 Answers1

1

you need to use a delegate to accomplish this. Overwrite the delegates paint event with the following:

QAlignmentDelegate.h

#include <QStyledItemDelegate>

class QAlignmentDelegate : public QStyledItemDelegate
{
public:

    explicit QAlignmentDelegate(Qt::Alignment alignment, QObject* parent = 0)
    : QStyledItemDelegate(parent),
    m_alignment(alignment)
    {

    }

    virtual void QAlignmentDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
    {
        QStyleOptionViewItem alignedOption(option);
        alignedOption.displayAlignment = m_alignment;
        QStyledItemDelegate::paint(painter, alignedOption, index);
    }

private:

    Qt::Alignment   m_alignment;                                                                    ///< Stores the alignment to use

};

Then simply assign the delegate to the view. In you mainWindow class (or wherever you create or use the view), the delegate can be used as follows:

MainWindow code

#include "QAlignmentDelegate.h"
...


QAlignmentDelegate* myDelegate = new QAlignmentDelegate(Qt::AlignmentCenter);
QTableView* myTableView = new QTableView(this);
myTableView->setItemDelegate(myDelegate);
myTableView->setModel(...); // start using the view

You can specify whatever Qt::Alignment (or combination of them using OR) when you create the delegate.

Alternatively, if you wrote/control the model code, you can implement the Qt::AlignmentRole and return 'Qt::AlignHCenter for data you want to be cetner-aligned.

Community
  • 1
  • 1
Nicolas Holthaus
  • 7,763
  • 4
  • 42
  • 97
  • im new to this, so not able to implement. Can you change the code using "tableWidget" , so that i can understand very well – shivcena Sep 28 '15 at 12:49
  • @sivanesan I've added an example of how to create the delegate and attach it to the view. – Nicolas Holthaus Sep 28 '15 at 13:08
  • @sivanesan ok, go ahead and update your question then – Nicolas Holthaus Sep 28 '15 at 14:34
  • @sivanesan well that won't work. You can't arbitrarily decide not to inherit from `QStyledItemDelegate`. Use my delegate exactly as I gave it to you. – Nicolas Holthaus Sep 28 '15 at 14:54
  • :After long struggle i got success but stylesheet is not working now – shivcena Sep 28 '15 at 15:53
  • that's to be expected: the delegate takes over the paint routine for the item, and so the old style-sheet won't apply. It's a solvable problem, but you'd be better off asking it in another question. – Nicolas Holthaus Sep 28 '15 at 16:33
  • http://stackoverflow.com/questions/32837036/how-to-add-stylesheet-in-delegate-class-in-qt , use this link for create new delegate class and to use in UI – shivcena Sep 29 '15 at 06:26