2014-09-19 101 views
0

我遇到了Qt信號和插槽的問題。我只是在學習Qt,但我有很多C++經驗。我從QTreeView派生了一個類,我想處理columnResized信號。插槽是永遠不會被調用,我看到了這個在「應用程序輸出」:將Qt基類信號映射到派生類中的插槽

QObject::connect: No such signal TRecListingView::columnResized(int,int,int) in ../ec5/reclistingwidget.cpp:142 

類的聲明看起來是這樣的:

class TRecListingView : public QTreeView 
{ 
    Q_OBJECT 
public: 
    TRecListingView(QWidget *parent, TTopicPtr topic); 
    ~TRecListingView(); 

private slots: 
    void onColumnResized(int index, int oldsize, int newsize); 

private: 
    TRecListingModel *Model = 0; 
}; 

在構造函數中我這樣做:

connect(this,SIGNAL(columnResized(int,int,int)), 
     this,SLOT(onColumnResized(int,int,int))); 

在我實現派生類之前,我已經早些工作了。然後我將信號映射到父窗口小部件中的插槽。

我試過運行qmake並重建項目。我也試過這個:

QTreeView *tv = this; 
connect(tv,SIGNAL(columnResized(int,int,int)), 
     this,SLOT(onColumnResized(int,int,int))); 

回答

1

columnResizedcolumnResized是不是信號,但插槽,所以你不能連接到它。

相反,你可以連接到QHeaderView::sectionResized

connect(this->horizontalHeader(),SIGNAL(sectionResized(int,int,int)), 
     this,     SLOT(onColumnResized(int,int,int))); 
+0

QTableView沒有horizo​​ntalHeader(),只是一個頭()。所以我得到了這個工作:connect(this-> header(),SIGNAL(sectionResized(int,int,int)), this,SLOT(onColumnResized(int,int,int))); – nicktook 2014-09-22 13:21:35

0

因爲它不是一個信號:

從技術文檔:

void QTreeView::columnResized (int column, int oldSize, int newSize) [protected slot] 

嘗試重新實現它:

#include <QTreeView> 
#include <QHeaderView> 
#include <QTimer> 
#include <QDebug> 


class TRecListingView : public QTreeView 
{ 
    Q_OBJECT 
public: 
    TRecListingView(QWidget *parent=0): 
     QTreeView(parent) 

    { 
     QTimer::singleShot(0, this, SLOT(fixHeader())); 
    } 


public slots: 

    void fixHeader() 
    { 
     QHeaderView *hv = new QHeaderView(Qt::Horizontal, this); 
     hv->setHighlightSections(true); 
     this->setHeader(hv); 
     hv->show(); 
    } 
protected slots: 
    void columnResized(int a, int b, int col) 
    { 
     qDebug() << "This is called"; 
    } 

public slots: 

}; 

簡單的用法:

TRecListingView trec; 

QStringList stringList; 
stringList << "#hello" << "#quit" << "#bye"; 
QStringListModel *mdl = new QStringListModel(stringList); 
trec.setModel(mdl); 
trec.show(); 

現在它工作正常,當你調整頭,你會看到許多This is called字符串。

+0

我試過了。該功能從未被調用過。 – nicktook 2014-09-19 20:51:53

+0

其實你不能覆蓋它,因爲它不是虛擬的,但你可以連接到'QHeaderView :: sectionResized'信號 – 2014-09-19 21:00:57

+0

@nicktook有一種方法來改變它。我完全重寫了我的答案,現在的例子有作品(我在我的電腦上測試過)。請看。 – Chernobyl 2014-09-20 05:01:00