Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to show custom widget (QFileDialog with QLineEdit) in TreeView's cell
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 4.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    WTMapot
    wrote on last edited by VRonin
    #1

    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&#40;&#41;
    {
        //return QFileDialog::getOpenFileName(0, tr("Open File"&#41;, " ", tr("Files (*.tif&#41;"&#41;);
    }
    
    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();*/
    
    
    1 Reply Last reply
    0
    • D Offline
      D Offline
      Dunkan
      wrote on last edited by
      #2

      Good afternoon!
      Faced the same problem. Did you find a solution?

      1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        Take a look at https://pastebin.com/XrppLZ3m

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        3

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved