QListView mehrzeilige Ausgabe [echtes Thema]

Alles rund um die Programmierung mit Qt
Antworten
Esleborn
Beiträge: 265
Registriert: 27. Januar 2005 01:23
Wohnort: Baden-Würtenberg
Kontaktdaten:

QListView mehrzeilige Ausgabe [echtes Thema]

Beitrag von Esleborn »

hi,
ich muss dafür sorgen, dass ein QListView mehrzeilige Ausgaben machen kann... kann mir da einer helfen?

Danke

Esleborn

PS: Sry für Unordnung mit den zwei Threads, aber die übersichtlichkeit des eigentlich zu führenden Threads hat doch deutlich gelitten...
Glaube an eine Lösung, nur dann kannst du auch eine finden.
patrik08
Beiträge: 746
Registriert: 27. Februar 2006 10:48
Wohnort: DE Freiburg

Re: QListView mehrzeilige Ausgabe [echtes Thema]

Beitrag von patrik08 »

Esleborn hat geschrieben:hi,
ich muss dafür sorgen, dass ein QListView mehrzeilige Ausgaben machen kann... kann mir da einer helfen?.
Dann sage auch dass du extra einen QModel geschrieben hast fuer diese Liste.. Richtig?
--#
Langjahrige programmierer suchen loesungen ... wo es immer moeglicht komplieziert ist... vom Thema http://www.qtforum.de/forum/viewtopic.php?t=2059 zusammen-gepackt und gelacht :D
Esleborn
Beiträge: 265
Registriert: 27. Januar 2005 01:23
Wohnort: Baden-Würtenberg
Kontaktdaten:

Beitrag von Esleborn »

das spielt keine Rolle.
QListView --> ein Model, ob selbst geschrieben oder nicht, ist wurscht.
Glaube an eine Lösung, nur dann kannst du auch eine finden.
Querdenker
Beiträge: 99
Registriert: 1. Dezember 2005 17:44
Wohnort: Karlsruhe

Beitrag von Querdenker »

Hi,

ich habe hier die Vesion 4.1.3. Da gibt es die QListView eigentlich nicht mehr,
schade.

Hast Du noch die Methode: SetItemDelegate()? Wenn ja,
kann ein eigenes QAbstractItemModel() hinzugefügt werden. Ich habe mal eines für ein QTableWidget gemacht, dessen Zellen alle mehrzeilig sind.

Die Delegate-Klasse sah so aus:

der Header:

Code: Alles auswählen

#ifndef MYITEM_H
#define MYITEM_H

#include <QItemDelegate>
#include <QList>

class QWidget;
class MyTable;

class MyItem : public QItemDelegate{
   Q_OBJECT
   	   
private:
   int                tmpTyp;
   MyTable            *table;
   bool               NoEdit;
   bool               HasAutoFilter;
   mutable QList<int> rowh;
   mutable int        row,column;
   
   void fillrowlist();
   
protected slots:
   void oneditchange(int);
   		   	   
public:
   MyItem(MyTable *parent, int art);
   QWidget *createEditor (QWidget * parent, 
	                       const QStyleOptionViewItem & option, 
					   		  const QModelIndex & index ) const;
								  
	void setEditorData(QWidget *editor, const QModelIndex &index) const;
	
   void setModelData(QWidget *editor, QAbstractItemModel *model,
                         const QModelIndex &index) const;
                          
   void drawDisplay(QPainter * painter, 
	                 const QStyleOptionViewItem & option, 
						  const QRect & rect, 
						  const QString & text ) const ;
                    
};
#endif
die Implementierung. Wobei: Die erste Zeile statts multiline Boxen eben Comboboxen anzeigt (wie Excel eben ;) ).

Code: Alles auswählen

#include "myitem.h"
#include <QTextEdit>
#include <QLineEdit>
#include <QModelIndex>
#include <QPainter>
#include <QComboBox>
#include <QTableWidgetItem>
#include <QRectF>
#include "dbengine.h"
#include "mytable.h"

void MyItem::oneditchange(int n){
   QString t;
   QComboBox *box = qobject_cast<QComboBox*>(QObject::sender());
   QTableWidgetItem *wi = 0;
   
	switch(n){
	   case 0:
	      for(int i=1; i<table->rowCount();i++){
	         t.setNum(rowh[i]);
	         table->verticalHeader()->resizeSection(i, rowh[i]);
	      }
	      break;
	   case 1:
	      for(int i=1; i<table->rowCount();i++){
	         wi = table->item(i,column);
	         if(wi != 0){
	            if(wi->text() != ""){
	               table->verticalHeader()->resizeSection(i, 0);
	            }
	         }
	      }
	      break;
	   case 2:
	      for(int i=1; i<table->rowCount();i++){
	         wi = table->item(i,column);
	         if(wi != 0){
	            if(wi->text() == ""){
	               table->verticalHeader()->resizeSection(i, 0);
	            }
	         }
	      }
	      break;
	   case 3:
	      break;
	   default:
	      t=box->itemText(n);
	      for(int i=1; i<table->rowCount();i++){
	         wi = table->item(i,column);
	         if(wi != 0){
	            if(wi->text() != t){
	               table->verticalHeader()->resizeSection(i, 0);
	            }
	         }
	      }
	      break;
	} 
};


void MyItem::drawDisplay(QPainter * painter, 
	                 const QStyleOptionViewItem & option, 
						  const QRect & rect, 
						  const QString & text ) const{
    int n = 0;
    QTableWidgetItem *wi = table->itemAt(rect.left(), rect.top());
    if(wi){
       painter->setFont(wi->font());
       painter->setPen(wi->textColor());
       n = wi->textAlignment(); 
       QRectF f = painter->boundingRect(rect, Qt::TextWordWrap | n, text);
	    painter->drawText(f, text);
	 }						 
};

void MyItem::setModelData(QWidget *editor, QAbstractItemModel *model,
                          const QModelIndex &index) const{
   QString t;
   try {
      if(index.row()==0 && table->GetAutoFilter()) return; 
      if(tmpTyp == 0){
         if(editor != 0) return;
	      QTextEdit *edit = static_cast<QTextEdit*>(editor);
	      if(edit !=0 )t = edit->toPlainText();
	   } else {
	      if(editor != 0) return;
	      QLineEdit *edit = static_cast<QLineEdit*>(editor);
	      if(edit !=0 )t = edit->text();
	   } 
	   model->setData(index, t, Qt::EditRole); 
	   table->DataBase()->SaveRow(table, table->GetTableName(), index.row()); 
	}
	catch(...){
	  return;
	}	                
};

void MyItem::setEditorData(QWidget *editor, const QModelIndex &index) const{
	try{
	   if(editor == NULL) return;
	   if(index.row()==0 && table->GetAutoFilter()) return;
	   if(table->HasColumnFilter(index.column())) return;
      QString value = index.model()->data(index, Qt::DisplayRole).toString();

      if(tmpTyp == 0){ 
        QTextEdit *edit = static_cast<QTextEdit*>(editor);
        edit->setPlainText(value);
      } else {
        QLineEdit *edit = static_cast<QLineEdit*>(editor);;
        edit->setText(value);
      }
   }
   catch(...){}
}; 

QWidget *MyItem::createEditor (QWidget * parent, 
	                       const QStyleOptionViewItem & option, 
								  const QModelIndex & index ) const{
	QWidget *edit;
	QString title;
	if(tmpTyp == 1){
	   title = "Gesamt";
	   edit = static_cast<QLineEdit*>(new QLineEdit(parent));
	   edit->setFont(option.font);	
	} else {
	   title = table->GetTableName();
	   edit = static_cast<QTextEdit*>(new QTextEdit(parent));
		edit->setFont(option.font);	
	}
	if(index.row()==0 && table->GetAutoFilter()){
	   
	   QComboBox *box = new QComboBox(parent);
	   QObject::connect(box, SIGNAL(activated(int)), this, SLOT(oneditchange(int)));
	   table->DataBase()->LoadAutoFilter(box,title,
		        table->horizontalHeaderItem(index.column())->text());
      rowh.clear();
      for(int i=0; i<table->rowCount(); ++i){
          rowh << table->rowHeight(i);
      }
      
      row=index.row();
      column=index.column();
      
		return box; 
	}
	if(table->HasColumnFilter(index.column())){
	   DocFilter *df;
	   df = table->GetDocFilter(index.column());
	   QComboBox *box = new QComboBox(parent);
	   table->DataBase()->LoadDocFilter(box, title, df->a, df->b, df->c);
	   return box;
	}
	return edit;					  
};

MyItem::MyItem(MyTable *parent, int nTyp):QItemDelegate(parent){
   tmpTyp = nTyp;
   NoEdit = false;
   table = parent;
};
Ich hoffe das hilft weiter. Florian Becker hat hier ein gutes Tut über Delegates gemacht. Für Dich interessant ist, wie QLineEdit mit QTextEdit vertauscht und gehandelt wird. Wie Text von und zum Editor geht.
e Grüssle au
Q... ;)
Esleborn
Beiträge: 265
Registriert: 27. Januar 2005 01:23
Wohnort: Baden-Würtenberg
Kontaktdaten:

Beitrag von Esleborn »

danke Querdenker für dne Code.
Das PRoblem ist nur
1. verwende ich views
2. scheint sich das so nicht auf listviews übertragen zu lassen. Mit dem Treeview klappt es, aber das listview will nicht...
Das paint der Delegate lässt es weiterhin einzeilig.
Glaube an eine Lösung, nur dann kannst du auch eine finden.
Esleborn
Beiträge: 265
Registriert: 27. Januar 2005 01:23
Wohnort: Baden-Würtenberg
Kontaktdaten:

Beitrag von Esleborn »

irgendwie sowas tut halbwegs...

Code: Alles auswählen

void C_HierachyDelegate::drawDisplay ( QPainter *fp_painter, const QStyleOptionViewItem &f_option,
	/* <-- */	/* <-- */	/* <-- */	const QRect &f_rect, const QString &f_text ) const
{
	QPen pen ( fp_painter->pen ( ) );
	QFont font ( fp_painter->font ( ) );
	QPalette::ColorGroup colorgroup ( f_option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled );

	QTextOption textOption ( f_option.decorationAlignment );
	textOption.setWrapMode ( QTextOption::WordWrap );

	QRect textRect = f_rect.adjusted ( 3, 0, -3, 0 ); // remove width padding
	textRect.setHeight ( qMax ( f_rect.height ( ), f_option.rect.height ( ) ) );
	textRect.setTop ( qMin ( f_rect.top ( ), f_option.rect.top ( ) ) );

	if ( f_option.state & QStyle::State_Selected )
	{
		fp_painter->fillRect ( f_rect, f_option.palette.brush ( colorgroup, QPalette::Highlight ) );
		fp_painter->setPen ( f_option.palette.color ( colorgroup, QPalette::HighlightedText ) );
	} else
		fp_painter->setPen ( f_option.palette.color ( colorgroup, QPalette::Text ) );

	if ( f_option.state & QStyle::State_Editing )
	{
		fp_painter->save ( );
		fp_painter->setPen ( f_option.palette.color ( colorgroup, QPalette::Text ) );
		fp_painter->drawRect ( f_rect.adjusted ( 0, 0, -1, -1 ) );
		fp_painter->restore ( );
	}

	fp_painter->setFont ( f_option.font );
	fp_painter->drawText ( textRect, f_text, textOption );

//	Painter zurücksetzen
	fp_painter->setFont ( font );
	fp_painter->setPen ( pen );
}
Glaube an eine Lösung, nur dann kannst du auch eine finden.
Esleborn
Beiträge: 265
Registriert: 27. Januar 2005 01:23
Wohnort: Baden-Würtenberg
Kontaktdaten:

Beitrag von Esleborn »

ok die Lösung ist ein Delegate:

Code: Alles auswählen

# include <QItemDelegate>

class C_HierachyDelegate : public QItemDelegate
{
   Q_OBJECT

public:
   C_HierachyDelegate ( QObject *fp_parent = NULL );

protected:
   virtual void drawDisplay ( QPainter *fp_painter, const QStyleOptionViewItem &f_option, const QRect &f_rect, const QString &f_text ) const;
   virtual void drawFocus ( QPainter *fp_painter, const QStyleOptionViewItem &f_option, const QRect &f_rect ) const;
};

Code: Alles auswählen

C_HierachyDelegate::C_HierachyDelegate ( QObject *fp_parent )
              : QItemDelegate ( fp_parent )
{
}

void C_HierachyDelegate::drawDisplay ( QPainter *fp_painter, const QStyleOptionViewItem &f_option,
   /* <-- */   /* <-- */   /* <-- */   const QRect &f_rect, const QString &f_text ) const
{
   QPen pen ( fp_painter->pen ( ) );
   QFont font ( fp_painter->font ( ) );
   QPalette::ColorGroup colorgroup ( f_option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled );

// This does not work; perhaps "setWrapMode" does change the alignment
//    QTextOption textOption ( f_option.decorationAlignment );
//    textOption.setWrapMode ( QTextOption::WordWrap );

// This does work...
   QTextOption textOption;
   textOption.setWrapMode ( QTextOption::WordWrap );
   textOption.setAlignment ( f_option.displayAlignment );

   QRect textRect = f_rect.adjusted ( 5, 0, -5, 0 ); // remove width padding
   textRect.setTop ( qMin ( f_rect.top ( ), f_option.rect.top ( ) ) );
   textRect.setHeight ( qMax ( f_rect.height ( ), f_option.rect.height ( ) ) );

   if ( f_option.state & QStyle::State_Selected )
   {
      fp_painter->fillRect ( f_rect, f_option.palette.brush ( colorgroup, QPalette::Highlight ) );
      fp_painter->setPen ( f_option.palette.color ( colorgroup, QPalette::HighlightedText ) );
   } else
      fp_painter->setPen ( f_option.palette.color ( colorgroup, QPalette::Text ) );

   if ( f_option.state & QStyle::State_Editing )
   {
      fp_painter->save ( );
      fp_painter->setPen ( f_option.palette.color ( colorgroup, QPalette::Text ) );
      fp_painter->drawRect ( f_rect.adjusted ( 0, 0, -1, -1 ) );
      fp_painter->restore ( );
   }

   fp_painter->setFont ( f_option.font );
   fp_painter->drawText ( textRect, f_text, textOption );

// Painter zurücksetzen
   fp_painter->setFont ( font );
   fp_painter->setPen ( pen );
}

void C_HierachyDelegate::drawFocus ( QPainter *fp_painter, const QStyleOptionViewItem &f_option, const QRect &f_rect ) const
{
   if ( f_option.state & QStyle::State_HasFocus )
   {
      QRect rect ( f_rect );
      rect.setTop ( qMin ( f_rect.top ( ), f_option.rect.top ( ) ) );
      rect.setHeight ( qMax ( f_rect.height ( ), f_option.rect.height ( ) ) );

      QStyleOptionFocusRect optionFocusRect;
      optionFocusRect.QStyleOption::operator= ( f_option );
      optionFocusRect.rect = rect;
      optionFocusRect.state |= QStyle::State_KeyboardFocusChange;
      QPalette::ColorGroup colorgroup = ( f_option.state & QStyle::State_Enabled ) ? QPalette::Normal : QPalette::Disabled;
      optionFocusRect.backgroundColor = f_option.palette.color ( colorgroup, ( f_option.state & QStyle::State_Selected )
                                             ? QPalette::Highlight : QPalette::Background );
      QApplication::style ( )->drawPrimitive ( QStyle::PE_FrameFocusRect, &optionFocusRect, fp_painter );
   }
}
Glaube an eine Lösung, nur dann kannst du auch eine finden.
Antworten