1

In my application I'm having a table widget. I created a new class to implement delegate for the table cells alignment as center. The delegate works fine, but stylesheet is not working properly. Below is how I implement the delegate class:

//i create new class with "QStyledItemDelegate" as base class
 //QAlignmentDelegate.h
#ifndef QALIGNMENTDELEGATE_H
#define QALIGNMENTDELEGATE_H

#include <QStyledItemDelegate>

class QAlignmentDelegate : public QStyledItemDelegate
{
 public:
   QAlignmentDelegate(Qt::Alignment alignment);
   virtual void paint(QPainter* painter,
   const QStyleOptionViewItem& option,
   const QModelIndex& index) const;

 private:
   Qt::Alignment  m_alignment;

};

#endif // QALIGNMENTDELEGATE_H

//QAlignmentDelegate.cpp
#include "qalignmentdelegate.h"

 QAlignmentDelegate::QAlignmentDelegate(Qt::Alignment alignment)
 {
  m_alignment=alignment;
 }

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

  //main.cpp
  QTableWidgetItem * protoitem = new QTableWidgetItem();
  protoitem->setTextAlignment(Qt::AlignRight);
  QTableWidgetItem * newitem = protoitem->clone();
  tableWidget->setItemPrototype(newitem);

Now how to implement stylesheet in this delegate class?

Lundin
  • 195,001
  • 40
  • 254
  • 396
shivcena
  • 2,023
  • 8
  • 24
  • 50
  • Did you check method [setStyleSheet](http://doc.qt.io/qt-5/qwidget.html#styleSheet-prop)? – KernelPanic Sep 29 '15 at 06:45
  • 1
    @KernelPanic: in that document says like it will not support for subclassing , am i right? – shivcena Sep 29 '15 at 06:54
  • Stylesheets are used for customizing widgets, not delegates. But you can use `QStylePainter`. But... Qt doesn't have complete support for direct painting customized controls. You may view at this question: http://stackoverflow.com/questions/19138100/how-to-draw-control-with-qstyle-and-with-specified-qss – Dmitry Sazonov Sep 29 '15 at 10:02
  • Show the *relevant* part of the style-sheet as well please. – Nicolas Holthaus Sep 29 '15 at 12:01

0 Answers0