dimanche 11 octobre 2020

no matching conversion for functional-style cast from 'const QString' to 'QTreeWidgetItem'

I am building a small application using QTreeWidgetItem and QTreeWidgetItem for practicing them and for practicing how to best and where use them. This is a small application where I am trying to assign an icon to a QTreeWidget using a helper function. Now for some reasons I get the following error :

no matching conversion for functional-style cast from 'const QString' to 'QTreeWidgetItem', below the code:

mainwindow.h

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    QTreeWidgetItem *createItem(const QString &name, const QString &iconPath);

    QString name() const {
        return m_Name;
    }

    QString iconPath() const {
        return m_Path;
    }

    void setName(const QString &name)
    {
        m_Name = name;
    }

    void setPath(const QString &path)
    {
        m_Path = path;
    }

private:
    Ui::MainWindow *ui;
    QString m_Name;
    QString m_Path;
};

mainwindow.cpp

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    auto *top1 = createItem("Images", "/path/to/icon.png");
    auto *top2 = createItem("Path", "/path/to/icon.png");
    top1->addChild(createItem("Original", "/path/to/icon.png"));
    top1->addChild(createItem("Sample", "/path/to/icon.png"));
    
    top2->addChild(createItem("Left Side", "/path/to/icon.png"));
    top2->addChild(createItem("Right Side", "/path/to/icon.png"));

    ui->treeWidget->addTopLevelItems({ top1, top2 });
}

MainWindow::~MainWindow()
{
    delete ui;
}


QTreeWidgetItem *MainWindow::createItem(const QString &name, const QString &iconPath)
{
    auto *item = QTreeWidgetItem(name);  // <-- Error Here
    item.setIcon(0, QIcon(iconPath));
    return item;
}

Now this is a compiler error and it seems that the constructor is missing QTreeWidgetItem and therefore is preventing me from using the helper function I created to help me define name and path for the small icon. I researched the problem and came across this source and also I read the additional source.

Thanks for helping me shedding light on this issue.

Aucun commentaire:

Enregistrer un commentaire