2014-12-02 119 views
0

我正在開發在C Qt的應用程序++如何通過點擊QPushbutton

我創建按鈕欄,TreeView的佈局......

樹視圖是我的主窗口定義更新QTreeWidget。 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); 
} 

我需要做的是找到一種方法與

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); 
    .... 
} 

這裏以文件的MyTree是頭文件:

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,因爲它也在另一個類中定義。

回答

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) { 
     .... 
    } 
}