2013-09-16 40 views
1

我此行我的代碼:沒有這樣的插槽QProgressBar :: setValue方法(const int的)

QObject::connect(scanning_worker, SIGNAL(update_progress_bar(const int)), ui.progress_bar, SLOT(setValue(const int))); 

,並在運行時我得到這個錯誤:

No such slot QProgressBar::setValue(const int) 

任何想法,爲什麼? 在文檔QT 4.8(我用)是setValuepublic slot ...

我嘗試這樣做:我在爭論int之前刪除const,但沒有改變。我試圖用調試器調用其他插槽,並在此插槽中找到了斷點,所以它沒問題。我也嘗試設置 '50' 作爲的setValue

的說法
QObject::connect(scanning_worker, SIGNAL(update_progress_bar(const int)), ui.progress_bar, SLOT(setValue(50))); 

,但仍是同樣的錯誤......

我的類:

class Scanning_worker : public QObject{ 
     Q_OBJECT 
    private: 
     int shots_count; 
    public: 
     Scanning_worker(const int shots) : shots_count(shots){} 
     ~Scanning_worker(){} 
    public slots: 
     void do_work(); 
    signals: 
     void error(const int err_num); 
     void update_progress_bar(int value); 
     void finished(); 
    }; 

而且ui.progress_bar是形式(孩子主窗口)...

的我在Visual Studio 2010中工作,W7教授和QT 4.8

+2

「我刪除了const之前int參數」 - 你是說,在SLOT(...)? – SingerOfTheFall

+0

no ... SLOT(update_progress_bar(int))... –

+3

它應該是:'QObject :: connect(scanning_worker,SIGNAL(update_progress_bar(int)),ui.progress_bar,SLOT(setValue(int)));' 。根本沒有'const'。 –

回答

6

插槽想要的int:你給它一個const int,因此錯誤。將SLOT(setValue(const int))更改爲SLOT(setValue(int))是不夠的。你需要改變你的信號,所以它有一個int的說法,而不是'const int的」太:

QObject::connect(scanning_worker, SIGNAL(update_progress_bar(int)), ui.progress_bar, SLOT(setValue(int))); 

基本上,你的信號應該始終有相同的論據,你的插槽,否則將無法正常工作。還有另一種方法將信號連接到插槽,如果你做錯了什麼,它會給你編譯時錯誤。比方說,你有一個這樣的類:

class Foo : public QObject { 
    Q_OBJECT 
public slots: 
    void fooSlot(){ } 
signals: 
    void fooSignal(){ } 
}; 
Foo *a = new Foo(); 

如果使用QT5則不用連接是這樣的:

connect(a, SIGNAL(fooSignal()), a, SLOT(fooSlot())); 

還可以連接這樣的:

connect( a, &Foo::fooSignal, a, &Foo::fooSlot); 

在這如果出現錯誤,則會在編譯期間顯示。它也有較少的括號,所以它更容易閱讀:P

+0

確定它包含在我的句子中,如果我刪除了SLOT中的「const」,我也在SIGNAL中刪除它...沒有更改 –

+0

@ Jana,你是否也在班上改變了信號的簽名,並檢查了你「發射」的論點? – SingerOfTheFall

+0

SingerOfTheFall:不,它不能解決我的問題 –

相關問題