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. How to access Ui::MainWindow member from sender object?
Forum Updated to NodeBB v4.3 + New Features

How to access Ui::MainWindow member from sender object?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 730 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.
  • A Offline
    A Offline
    anarelle
    wrote on last edited by
    #1

    I'm working on an application that has several QPushButtons which perform very similar actions, so I'm trying to capture all of the on_click actions with one slot that fits all.
    In my window constructor I have

    ui(new Ui::MainWindow){
    ui->setupUi(this);
    

    and then I connected all my buttons to the on_button_clicked() slot, like this:

    connect(ui->btn01, SIGNAL(clicked()), this, SLOT(on_button_clicked()));
    connect(ui->btn02, SIGNAL(clicked()), this, SLOT(on_button_clicked()));
    ...
    

    The on_button_clicked() slot is successfully capturing which button was pressed and then, based on that I get a file name from a QHash where I have all the button object names as keys. I'm trying to use that file name as the background image I want to set to the button:

    //get the button that was pressed
        QPushButton* pressed_btn=qobject_cast<QPushButton*>(sender());
        QString btn_name=pressed_btn->objectName();
    
        //get the image associated to that button in the map
        QString img=images[btn_name];
    
        //set a background using the image associated to the pressed button
        ui->pressed_btn->setStyleSheet("#"+btn_name+"{ background-image: url(://"+img+") }");
    

    However, I get an error saying that pressed_btn is not a member of ui, which is true. The members are btn01, btn02, btn03, etc. so I need to get the proper name that is associated to pressed_btn. I've also tried using ui->btn_name but of course I get the same error, as I need the name inside that variable, not the actual variable identifier (btn_name).
    Is there a way to dereference the actual object from the sender object I'm getting, so I can use it as a member of the ui object?

    aha_1980A 1 Reply Last reply
    0
    • A anarelle

      I'm working on an application that has several QPushButtons which perform very similar actions, so I'm trying to capture all of the on_click actions with one slot that fits all.
      In my window constructor I have

      ui(new Ui::MainWindow){
      ui->setupUi(this);
      

      and then I connected all my buttons to the on_button_clicked() slot, like this:

      connect(ui->btn01, SIGNAL(clicked()), this, SLOT(on_button_clicked()));
      connect(ui->btn02, SIGNAL(clicked()), this, SLOT(on_button_clicked()));
      ...
      

      The on_button_clicked() slot is successfully capturing which button was pressed and then, based on that I get a file name from a QHash where I have all the button object names as keys. I'm trying to use that file name as the background image I want to set to the button:

      //get the button that was pressed
          QPushButton* pressed_btn=qobject_cast<QPushButton*>(sender());
          QString btn_name=pressed_btn->objectName();
      
          //get the image associated to that button in the map
          QString img=images[btn_name];
      
          //set a background using the image associated to the pressed button
          ui->pressed_btn->setStyleSheet("#"+btn_name+"{ background-image: url(://"+img+") }");
      

      However, I get an error saying that pressed_btn is not a member of ui, which is true. The members are btn01, btn02, btn03, etc. so I need to get the proper name that is associated to pressed_btn. I've also tried using ui->btn_name but of course I get the same error, as I need the name inside that variable, not the actual variable identifier (btn_name).
      Is there a way to dereference the actual object from the sender object I'm getting, so I can use it as a member of the ui object?

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by aha_1980
      #2

      Hi @anarelle, welcome!

      I think you have multiple options here:

      1. pressed_btn is a pointer, so pressed_btn->setStyleSheet("#"+btn_name+"{ background-image: url(://"+img+") }"); should at least compile, probably also work
      2. There is QSignalMapper, which allows to put an additional parameter to your slot
      3. With the new functor-style connects you can use lambdas or std::bind to pass additional parameters to the slot

      Regards

      Qt has to stay free or it will die.

      1 Reply Last reply
      3
      • A Offline
        A Offline
        anarelle
        wrote on last edited by
        #3

        Thanks! Simply using pressed_btn->setStyleSheet did the trick (compiled and also worked)!

        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