2013-11-26 54 views
1

我夥計們,
的Qt 5.1版本
我現有的QT項目工作,我不明白爲什麼當我點擊按鈕,就不能在插槽中啓動功能Qpush按鈕 - >點擊信號從QPushButton沒有火起來的功能插槽

我想要做的是我的Qt應用程序連接到網絡,並使用QTreewidget顯示所有機器與他們的MAC地址。這部分工作正常 我的巢任務是從上面的步驟中創建的QTree對象中選擇mac-adrress,然後創建一個按鈕以在該機器上啓動升級過程。
下面是我的源代碼

1)的main.cpp

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 
    Regular_Work w; 
    w.show(); 
    return a.exec(); 
} 

2)regular_work.cpp

Regular_Work::Regular_Work(QWidget *parent) 
    : QMainWindow(parent),macAdd_tree_p_(0), reg_upgrade_button_(0), 
     box_table_p_(0), 
     udp_listner_p_(0) 

{ 
    ui.setupUi(this); 
    // Create the main view 
    create_main_view(this, macAdd_tree_p_, box_table_p_ , reg_upgrade_button_); 

    init(); // this function upgradels other signals and slots from other class to find the network and upgradel the slots which displays teh tree view of mac address connected to a network. 

    create_menu_actions(); 
} 
Regular_Work::~Regular_Work() 
{ 

} 

// this function is called from another slot when itemClicked signal is received from QTreeWidgetItem 
void Regular_Work::set_reg_upgrade_button_visible(Regular_Work* main_p) 
{ 
    QPushButton* reg_upgrade_button = new QPushButton ("Regular_upgradei"); 
    reg_upgrade_button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding) ; 
    QWidget* centralWidget = new QWidget(main_p); 

    centralWidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); 
    QHBoxLayout* layout = new QHBoxLayout(centralWidget); 
    layout->addWidget(reg_upgrade_button_); 
    main_p->setCentralWidget(centralWidget); 
    reg_upgrade_button->setVisible(true); 
    connect(reg_upgrade_button, SIGNAL(clicked()), main_p, SLOT(start_Work("start Work"))); // this is teh problem ? 

} 
void Regular_Calibration::start_Work(const QString& error_message) 
{ 
    QMessageBox::information(this, 
           tr("Push button works "), 
           error_message); 
} 

非常感謝您的幫助

回答

1

你的問題就出在事實上你正試圖在connect語句中傳遞一個參數值。這是行不通的: -

SLOT(start_Work("start Work")) 

您需要做的是創建一個與clicked()信號參數匹配的插槽。然後在那個槽功能中,你可以調用你的start_Work(「start Work」)函數。事情是這樣的: -

class Regular_Work : public QMainWindow 
{ 
    Q_OBJECT 

    private: 
     void start_Work(const QString&); 

    private slots: 
     void ReceiveButtonClicked(); 
}; 

void RegularWork::ReceivedButtonClicked() 
{ 
    start_Work("start Work"); 
} 

連接信號和插槽: -

connect(reg_upgrade_button, SIGNAL(clicked()), main_p, SLOT(ReceiveButtonClicked())); 

如果您使用Qt 5,您可以使用新的連接語法: -

connect(reg_upgrade_button, &QPushButton::clicked, main_p, &RegularWork::ReceiveButtonClicked); 

這有在編譯時告訴你是否有問題以及指向函數的好處,所以你不需要爲函數指定任何參數。

+0

@ Merlin09非常感謝Merlin,你的解釋非常好,以及你的代碼。 – samprat

+0

不客氣; O) – TheDarkKnight

相關問題