[gelöst] treeView item QString

Alles rund um die Programmierung mit Qt
Antworten
central
Beiträge: 14
Registriert: 1. Juli 2005 02:00
Wohnort: Hamburg
Kontaktdaten:

[gelöst] treeView item QString

Beitrag von central »

Guten Morgen,

wie bekomme ich den markierten Eintrag aus meinem Treeview zum Beispiel als QString in ein QLabel .

Code: Alles auswählen

label1->setText(treeView->currentItem());
geht ja leider nicht :)

ein wenig code zum verständnis

Code: Alles auswählen

QDirModel *model = new QDirModel;
treeView->setModel(model); 
treeView->setSelectionBehavior(QAbstractItemView::SelectItems);
treeView->header()->setClickable(true);

QModelIndex parentIndex = model->index(QDir::rootPath());
int numRows = model->rowCount(parentIndex);
        for (int row = 0; row < numRows; ++row) {
            QModelIndex index = model->index(row, 0, parentIndex);
	    QString text = model->data(index, Qt::DisplayRole).toString();
	}
treeView->setRootIndex(parentIndex);
Wäre wirklich dankbar für jeden Hilfe.

gruß

central
Zuletzt geändert von central am 29. Oktober 2006 17:38, insgesamt 1-mal geändert.
no trouble
no fun
caligano
Beiträge: 126
Registriert: 19. August 2006 15:33

Beitrag von caligano »

Hallo!

Hier mal ein bisschen Code, ist aber ungetestet!

Code: Alles auswählen

connect(treeview->selectionModel(), SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),  this, SLOT(displayText(const QModelIndex &)));

void Klasse::displayText(const QModelIndex &index)
{
	QString fileName = model->fileName(index);
   label1->setText(fileName);
}
upsala
Beiträge: 3946
Registriert: 5. Februar 2006 20:52
Wohnort: Landshut
Kontaktdaten:

Beitrag von upsala »

Oder für eine einmalige Darstellung:

Code: Alles auswählen

label1->setText((treeView->currentIndex()).data());
central
Beiträge: 14
Registriert: 1. Juli 2005 02:00
Wohnort: Hamburg
Kontaktdaten:

Beitrag von central »

Hallo beides funktioniert leider nicht :(
upsala hat geschrieben: label1->setText((treeView->currentIndex()).data());
bringt

Code: Alles auswählen

Fehler: keine passende Funktion für Aufruf von »QLabel::setText(QVariant)«
und bei der Lösung von caligano

habe ich in meinem Header

Code: Alles auswählen

virtual void          copy_Slot(const QModelIndex &index);
der connect ist meinem constructor .

der aufruf

Code: Alles auswählen

void Klasse::copy_Slot(const QModelIndex &index) 
{ 
   QString fileName = model->fileName(index); 
   label1->setText(fileName); 
}
ergibt

Code: Alles auswählen

Fehler: »model« wurde in diesem Gültigkeitsbereich nicht definiert
wie mache ich den model bekannt wenn nicht im construtor?

hier mal meine main

Code: Alles auswählen

#include <QApplication>
#include <QtGui>
#include "mywdg.h"

 int main(int argc, char *argv[])

  {        QApplication app(argc, argv);

Q_INIT_RESOURCE(mywidget);
   myWDG *win = new myWDG();

win->show();

     return app.exec();
  };

mein widget header

Code: Alles auswählen

class myWDG : public QWidget , private Ui::myWidget
  {
    Q_OBJECT

  public:

myWDG( );

    /*$PUBLIC_FUNCTIONS$*/

  private slots:
    /*$PUBLIC_SLOTS$*/
    virtual void	  multi_Slot();
 virtual void          combobox_Slot();
    virtual void          help_Slot();
    virtual void          delete_Slot();
    virtual void          move_Slot();
    virtual void          copy_Slot(const QModelIndex &index);


protected:
    /*$PROTECTED_FUNCTIONS$*/
QProcess *proc;
QString Fpath,Ppath;
QTimer timer;
  protected slots:
    /*$PROTECTED_SLOTS$*/
};
hier mywdg.cpp

Code: Alles auswählen


myWDG::myWDG( ): QWidget()
{

setupUi( this );

QDirModel *model = new QDirModel;
//QTreeView *tree = new QTreeView(treeView);
 
treeView->setModel(model); 
treeView->setSelectionBehavior(QAbstractItemView::SelectItems);
treeView->header()->setClickable(true);

QModelIndex parentIndex = model->index(QDir::rootPath());
int numRows = model->rowCount(parentIndex);
        for (int row = 0; row < numRows; ++row) {
            QModelIndex index = model->index(row, 0, parentIndex);
	    QString text = model->data(index, Qt::DisplayRole).toString();
	QString fileName = model->fileName(index); 
	}
	
treeView->setRootIndex(parentIndex);

  connect(helpB, SIGNAL(clicked()), this, SLOT (help_Slot()));
connect( comboBox2, SIGNAL( activated(int) ), this, SLOT( combobox_Slot() ) );
connect( SendB, SIGNAL( clicked() ), this,SLOT( multi_Slot() ) );
connect(treeView->selectionModel(), SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),  this, SLOT(copy_Slot(const QModelIndex &)));
connect( SendB, SIGNAL( clicked() ), this,SLOT( multi_Slot() ) );
   connect( deleteB, SIGNAL( clicked() ), this, SLOT( delete_Slot() ) );
}

void myWDG::copy_Slot(const QModelIndex &index)
{

 QString fileName = model->fileName(index); 
  textLabel1_2->setText(fileName);
}
was mache ich verkehrt was muss ich ändern ?

gruß

central

Using Qt version 4.1.4
no trouble
no fun
caligano
Beiträge: 126
Registriert: 19. August 2006 15:33

Beitrag von caligano »

Hallo!
wie mache ich den model bekannt wenn nicht im construtor?
Versuch mal folgendes:

header:

Code: Alles auswählen

class myWDG : public QWidget , private Ui::myWidget
  {
    Q_OBJECT

  public:

myWDG( );

    /*$PUBLIC_FUNCTIONS$*/

  private slots:
    /*$PUBLIC_SLOTS$*/
    virtual void     multi_Slot();
 virtual void          combobox_Slot();
    virtual void          help_Slot();
    virtual void          delete_Slot();
    virtual void          move_Slot();
    virtual void          copy_Slot(const QModelIndex &index);


protected:
    /*$PROTECTED_FUNCTIONS$*/
QProcess *proc;
QString Fpath,Ppath;
QTimer timer;
  protected slots:
    /*$PROTECTED_SLOTS$*/


//das hier hinzufügen:

private:
QDirModel *model;
QTreeView *treeview;

};
.cpp

Code: Alles auswählen

...
model = new QDirModel;
treeview = new QTreeView();
...
central
Beiträge: 14
Registriert: 1. Juli 2005 02:00
Wohnort: Hamburg
Kontaktdaten:

Beitrag von central »

Danke caligano,
so geht es jetzt :D
wenn man weiß wie ist es auch nicht mehr so schwer .

dank auch an upsala.

gruß

central
no trouble
no fun
Antworten