2016-09-15 45 views
-2

參數我有一個Qt工程的Windows用戶界面,我想這個UI的參數傳遞到另一個函數,這樣的:通UI在Qt的

ConfWindows::ConfWindows(QWidget *parent) : 
QDialog(parent), 
ui(new Ui::ConfWindows) 
{ 
    ui->setupUi(this); 

    connect(ui->add_button, SIGNAL(clicked()), this, SLOT(add_elem(ui->name_edit))); 
} 

void  add_elem(QLabel test) 
{ 
    qDebug() << test.text(); 
} 

,但我得到一個錯誤,當我嘗試做即:

'QLabel::QLabel(const QLabel&)' is private 
Q_DISABLE_COPY(QLabel) 

在參數中傳遞UI元素是不可能的?

感謝您的幫助。

+1

傳遞'const&'? – LogicStuff

+0

是這樣的? (void)add_elem(QLabel const&test) { qDebug()<< test.text(); }' 我得到這個錯誤: '未定義參考ConfWindows :: add_elem(QLabel常量&)' 但是,add_elem是一個插槽,所以他不需要 「ConfWindows ::」 以前吧? – R3J3CT

+0

不應該是'SLOT(add_elem(QLabel)));'首先呢?另外,複製被禁用,通過const引用傳遞。 – xinaiz

回答

1

首先,將你的UI作爲參數傳遞並不是一個好習慣。 其次,你應該在插槽的函數原型中不能有比你的信號更多的參數。

我應該這樣做: 我會使add_elem函數,ConfWindows類的成員函數,以便有權訪問其內部的UI並獲取標籤的文本。

ConfWindows::ConfWindows(QWidget *parent) : 
QDialog(parent), ui(new Ui::ConfWindows) { 
    ui->setupUi(this); 

    connect(ui->add_button, SIGNAL(clicked()), this, SLOT(add_elem())); 
} 

void ConfWindows::add_elem() { 
    QString text = ui->label->text(); 
    qDebug() << test.text(); 
}