Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved why doesn't this connect() function work?

    General and Desktop
    2
    3
    333
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J
      JackAlredyInUse last edited by

      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 .

      J.Hilk 1 Reply Last reply Reply Quote 0
      • J.Hilk
        J.Hilk Moderators @JackAlredyInUse last edited by

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

        add the following below firstColorButton = new QToolButton()

        firstColorButton ->setCheckable(true);
        

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

        Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply Reply Quote 0
        • J
          JackAlredyInUse last edited by

          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

          1 Reply Last reply Reply Quote 1
          • First post
            Last post