如何通过点击QPush按钮更新QTreeWidget

How to update a QTreeWidget by Clicking on QPushbutton

本文关键字:按钮 更新 QTreeWidget QPush 何通过      更新时间:2023-10-16

我正在开发一个Qt应用程序C++

我已经创建了一个带有按钮栏的布局,树视图...

树视图在我的主窗口中定义.cpp

MainWindow::MainWindow()
{
    setWindowTitle(QString::fromUtf8("PULS"));
    resize(800,600);
    setUnifiedTitleAndToolBarOnMac(true);
    createDeviceStatusBar();
    createSectionBar();
    createTreeView();
    QWidget *MainWindowWidget = new QWidget();
    QVBoxLayout *MainWindowLayout = new QVBoxLayout(MainWindowWidget);
    BrowserSection = new QGroupBox();
    QWidget *BrowserWidget = new QWidget();
    QHBoxLayout *BrowserLayout = new QHBoxLayout(BrowserWidget);
    BrowserLayout->addWidget(SectionBar);
    BrowserLayout->addWidget(TreeSection);
    BrowserSection->setLayout(BrowserLayout);
    MainWindowLayout->addWidget(DeviceSection);
    MainWindowLayout->addWidget(BrowserSection);
    setCentralWidget(MainWindowWidget);
    show();
}

createSectionBar() 是一个布局,定义如下:

void MainWindow::createSectionBar()
{
    SectionBar = new QGroupBox();
    QVBoxLayout *SectionBarlayout = new QVBoxLayout;
    SectionBarlayout->setContentsMargins(QMargins(0,0,0,0));
    MusicButton = new QPushButton();
    MusicButton->setFixedSize(110,150);
    MusicButton->setIcon(QIcon(":/images/music.png"));
    MusicButton->setIconSize(QSize(90,144));
    MusicButton->setFlat(true);
    MusicButton->setAutoFillBackground(true);
    connect(MusicButton, SIGNAL(clicked()), this, SLOT(MusicTreeFile()));

    SectionBarlayout->addWidget(MusicButton);

    SectionBar->setLayout(SectionBarlayout);
}

createTreeView() 定义如下,以启用 TreeView 和 Items。

void MainWindow::createTreeView()
{
    TreeSection = new QGroupBox();
    QVBoxLayout *TreeLayout = new QVBoxLayout;
    MyTree = new TreeWidget();
    MyTree->setSortingEnabled(true);
    MyTree->setColumnWidth(0, 400);
    QTreeWidgetItem* headerItem = new QTreeWidgetItem();
    headerItem->setText(0,QString("File Name"));
    headerItem->setText(1,QString("Size (Bytes)"));
    headerItem->setText(2,QString("Date"));
    MyTree->setHeaderItem(headerItem);
    MyTree->setAutoFillBackground(true);
    TreeLayout->addWidget(MyTree);
    TreeSection->setLayout(TreeLayout);
}

我需要的是找到一种方法来提交MyTree

while (file != NULL && file->parent_id == folder_parent) {
    QTreeWidgetItem* item = new QTreeWidgetItem();  
    int i;  
    LIBMTP_file_t *oldfile;                 
    item->setText(0,file->filename);    
    Tree->addTopLevelItem(item);
    ....
}

这是头文件:

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow();
protected:
private slots:
    void MusicTreeFile();
private:
    void createSectionBar();
    void createTreeView();
    void createDeviceStatusBar();
    QGroupBox *SectionBar;
    QGroupBox *TreeSection;
    QGroupBox *DeviceSection;
    QGroupBox *BrowserSection;
    QPushButton *MusicButton;
    TreeWidget *MyTree;
};

但只有在单击MusicPushButton时才需要完成,我已经将MusicTreeFile()连接到PushButton()操作。 但是如何访问MyTree,因为它也在另一个类中定义。

您不将信号连接到类。将其连接到类的实例。您的两个实例都在 MainWindow 中定义。因此,在创建两个小部件后,您可以调用

QObject::connect(this->MusicButton, SIGNAL(clicked()), this->MyTree, SLOT(slot_name()));

slot_name为功能,成为您的职能

TreeWidget::slot_name{
    while (file != NULL && file->parent_id == folder_parent) {
        ....
    }
}