如何通过单击QPushbutton更新QTreeWidget

时间:2014-12-02 08:20:37

标签: c++ qt treeview

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

我创建了一个带按钮栏的布局,TreeView ......

Treeview在我的MainWindow.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,因为它也在另一个类中定义..

1 个答案:

答案 0 :(得分:0)

您没有将信号连接到班级。您将它连接到类的实例。您的两个实例都在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) {
        ....
    }
}