QComboBox und Alignment

Alles rund um die Programmierung mit Qt
Antworten
ole
Beiträge: 15
Registriert: 22. Dezember 2009 11:21

QComboBox und Alignment

Beitrag von ole »

Hallo,

Gibt es eine Möglichkeit (z.B. mit Style Sheets) den Text in der QComboBox zentriert auszurichten? Also ich meine nicht den Text in der Popup Box, sondern den aktuellen.

Danke!
ole
Beiträge: 15
Registriert: 22. Dezember 2009 11:21

Beitrag von ole »

Hab eine mögliche Lösung gefunden.
Man erreicht einen ähnlichen Effekt, wenn man die QComboBox editierbar macht und das dadurch erreichbare QLineEdit disabled und anpasst.

Gruß
russkij
Beiträge: 57
Registriert: 14. Dezember 2005 11:57

Re: QComboBox und Alignment

Beitrag von russkij »

Diese Methode hat den Effekt, dass die Combobox trotzdem als eine Editierbare ausschaut.

Es gibt noch folgende Möglichkeit:

Code: Alles auswählen


class MyCombo : public QComboBox
{
public:
   MyCombo(QWidget *parent) : QComboBox(parent), m_arrowWidth(18)
   {
   }
   void MyCombo::paintEvent ( QPaintEvent * event )  
   {
      //return QComboBox::paintEvent(event);
      QStylePainter painter(this);
      painter.setPen(palette().color(QPalette::Text));
      QStyleOptionComboBox opt;
      initStyleOption(&opt);
      painter.drawComplexControl(QStyle::CC_ComboBox, opt);
      opt.direction = Qt::RightToLeft;

      if( style() )
      {
         QRect rc = style()->subControlRect(QStyle::CC_ComboBox, &opt, QStyle::SC_ComboBoxArrow, this);
         m_arrowWidth = rc.width();
      }
      opt.rect.setRight(opt.rect.right() - m_arrowWidth);
      painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
   }

private:
   int m_arrowWidth;
};
class MyDelegate : public QItemDelegate
{
public:
   MyDelegate(QWidget *parent): QItemDelegate(parent)
   {}
   void paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const   
   {
      QStyleOptionViewItem myOption = option;

      myOption.rect.setRight(option.rect.right() - 18);  //@TODO breite des pfeiles
      QItemDelegate::paint(painter, myOption, index);
   }
};


 MyCombo *mComboBox3 = new MyCombo(dlg1);
   mComboBox3->setItemDelegate(new MyDelegate(mComboBox3));
   mComboBox3->addItem("1.000");
   mComboBox3->addItem("2.000");
   mComboBox3->addItem("345.000");
   mComboBox3->addItem("9.000");

   for(int i = 0; i < mComboBox3->count(); i++)
      mComboBox3->setItemData(i, Qt::AlignRight, Qt::TextAlignmentRole);

Antworten