質問

i Guys ,
Qt Version 5.1
I am working on existing qt project and I failed to understand why when I click on button , it fails to fire up function in slot

What I am trying to do is my Qt application connects to the network and displays all the machine with their mac address using QTreewidget. This part works fine my nest task is to select the mac-adrress from the QTree object created in above step AND then create a pushbutton to start the upgrade process on that machine.
Below is my source code

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

Thanks a lot for the help

役に立ちましたか?

解決

Your problem lies in the fact that you're trying to pass a parameter value in the connect statement. This can not work: -

SLOT( start_Work( "start Work" ) )

What you need to do is to create a slot that matches the arguments of the clicked() signal. Then in that slot function, you can call your start_Work("start Work") function. Something like this: -

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 the signal and slots: -

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

If you use Qt 5, you can use the new connection syntax: -

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

This has the advantage of telling you if there's a problem at compile time, as well as taking pointers to functions, so you don't specify any arguments for the functions.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top