why doesn't this connect() function work?



  • I want to use lambda expression in my function,the relations between files and classes like follows:

    //main.cpp
    MainWIndow w;
    w.show()
    
    //MainWIndow.h
    class MainWindow:public QMainWindow
    {
              void initMainWindowUI();
    }
    
    //MainWindow.cpp
    void MainWindow::initMainWIndowUI()
    {
       QAction *colors = new QAction(ui->menuEdit);
       // this connect() work well 
       connect(colors,&QAction::triggered,this,&MainWindow::doActionColorGradient);
       ui->menuEdit->addAction(colors);
    }
    void MainWindow::doActionColorGradient()
    {
       QSharedPointer<QDialog> colorGradientParamInputDlg(new QDialog());
       QColor firstColor;
       QToolButton *firstColorButton;
       firstColorButton = new QToolButton();
       // there is nothing happened when I clicked the firstColorButton.
       // what's wrong with my code?
       QObject::connect(firstColorButton,&QToolButton::triggered,[&]{
    
         firstColor = QColorDialog::getColor(QString("first color"));
    
       });
       
    }
    

    Appreciate if you can tell me the solutions .



  • You're using &QToolButton::toggled, but a thats not enabled by default.

    add the following below firstColorButton = new QToolButton()

    firstColorButton ->setCheckable(true);
    


  • Thank you,But it seems not effct,and I edit

    QObject::connect(firstColorButton,&QToolButton::triggered,...
    

    to

    QObject::connect(firstColorButton,&QPushButton::clicked,...
    

    then it works.
    And I'm learning the differences between triggered and clicked. XD


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.