Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. why doesn't this connect() function work?
QtWS25 Last Chance

why doesn't this connect() function work?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 471 Views
  • 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 Offline
    J Offline
    JackAlredyInUse
    wrote on last edited by
    #1

    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.HilkJ 1 Reply Last reply
    0
    • J JackAlredyInUse

      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.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      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


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

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JackAlredyInUse
        wrote on last edited by
        #3

        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
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved