How to show custom widget (QFileDialog with QLineEdit) in TreeView's cell



  • Hi All

    I have a custom widget named named FileSelector made up of a toolbutton and QlineEdit. I want this widget to display inside a tableview for a user to select a file using Qfiledialog. The file dialog is triggered by the toolbutton using the static function QFileDialog::getOpenFileNames. I have created the table view and all works well. I can select the file and it displays in the tableview.The problem is when I click close or accept on the tableview the application crashes....Any help would be appreciated.

    @/// File selector class widget for opening files from a directory

    class FileSelector : public QWidget
    {
    Q_OBJECT

    public:

    /// Constructor
    FileSelector(QWidget *parent = 0);
    
    /// Destructor
    virtual ~FileSelector();
    
    QToolButton *m_tbutton;
    
    QLineEdit *m_edit;
    
    
    QString getFile();
    
    //Parameter *parameter;
    
    //void setParamValue(QString &text){parameter->value = text.toStdString();}
    

    signals:

    void getFileName(QLineEdit *);
    

    public slots:

    /// Function to open a file from a directory
    void selectFile();
    
    void setParamValue(const QString &);
    

    protected:

    //QFileDialog *filedialog;
    

    private:

    };@

    @FileSelector::FileSelector(QWidget *parent) : QWidget(parent)
    {
    //ctor
    //parameter = param;
    m_tbutton = new QToolButton(this);
    m_tbutton->setFocusPolicy(Qt::StrongFocus);
    m_edit = new QLineEdit(this);
    //filedialog = new QFileDialog(0);

    QHBoxLayout *hb = new QHBoxLayout();
    hb->setContentsMargins(0, 0, 0, 0);
    hb->setSpacing(0);
    hb->addWidget(m_edit);
    hb->addWidget(m_tbutton);
    setLayout(hb);
    
    connect(m_tbutton, SIGNAL(clicked()), this, SLOT(selectFile()));
    //connect(m_edit, SIGNAL(textChanged(const QString &)), this, SLOT(setParamValue(const QString &)));
    

    }

    FileSelector::~FileSelector()
    {
    //dtor
    }

    void FileSelector::selectFile()
    {
    //emit getFileName(m_edit);
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), " ", tr("Files (*.tif)"));
    //if(!fileName.isEmpty())
    //{
    //m_edit->setText(fileName);
    //}
    //cout << fileName.toStdString() << " [[[" << endl;
    //item->setText(fileName);
    }

    QString FileSelector::getFile()
    {
    //return QFileDialog::getOpenFileName(0, tr("Open File"), " ", tr("Files (*.tif)"));
    }

    void FileSelector::setParamValue(const QString &text)
    {
    //parameter->value = text.toStdString();
    //cout << text.toStdString() << " : " << endl;
    }@

    @class FileSelectorDelegate : public QItemDelegate
    {
    Q_OBJECT

    public:

    FileSelectorDelegate(QObject *parent = 0);
    
    virtual ~FileSelectorDelegate(){};
    
    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 updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex &index) const;
    
    //void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    

    protected:

    private:

    };
    @

    @#include "fileselectordelegate.h"

    FileSelectorDelegate::FileSelectorDelegate(QObject *parent): QItemDelegate(parent)
    {

    }

    QWidget *FileSelectorDelegate::createEditor(QWidget parent,const QStyleOptionViewItem &/ option /,const QModelIndex &/ index */) const
    {
    FileSelector *editor = new FileSelector(parent);
    editor->m_edit->setText("Open File");

    return editor;
    

    }

    void FileSelectorDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const
    {
    QString value = index.model()->data(index, Qt::EditRole).toString();

    FileSelector *sel = static_cast<FileSelector*>(editor);
    sel->m_edit->setText(value);
    

    }

    void FileSelectorDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,const QModelIndex &index) const
    {
    FileSelector sel = static_cast<FileSelector>(editor);
    //sel->interpretText();
    QString value = sel->m_edit->text();

    model->setData(index, value, Qt::EditRole);
    

    }

    void FileSelectorDelegate::updateEditorGeometry(QWidget editor,const QStyleOptionViewItem &option, const QModelIndex &/ index */) const
    {
    editor->setGeometry(option.rect);
    }@

    this code crashes when I close the table view after selecting the file. The code which creates the table is as follows

    @ // Box layout to arrange the widgets in a vertical order

    m_model = new QStandardItemModel();
    m_delegate = new FileSelectorDelegate;
    m_model->setRowCount(plist.size());
    m_model->setColumnCount(2);
    
    m_dg.tableView->setModel(m_model);
    m_dg.tableView->setItemDelegate(m_delegate);
    //m_dg.tableView->setItemDelegateForColumn(1, m_delegate);
    //FileSelector *fselector = new FileSelector(this);
    
    for(unsigned int i = 0; i < plist.size(); ++i)
    {
        QModelIndex index1 = m_model->index(i, 0, QModelIndex());
        QModelIndex index2 = m_model->index(i, 1, QModelIndex());
    
        m_model->setData(index1, QVariant(QString::fromStdString(plist[i].name)));
    }
    
    for ( int i = 0; i < m_model->rowCount(); ++i )
    {
        m_dg.tableView->openPersistentEditor(m_model->index(i, 1));
    }
    m_dg.tableView->resizeColumnToContents(0);
    m_dg.tableView->resizeRowsToContents();
    //m_dg.tableView->show();*/@

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.