2

I want to send two integers, string and fret, to a SLOT that will process the location of the button that was pressed. The SIGNAL and SLOT argument have to match so I am thinking I need to reimplement the QPushButton::clicked event method. Problem is I am new to Qt and could use some direction.

connect(&fretBoardButton[string][fret], SIGNAL(clicked()), this, SLOT     (testSlot()));
Ritchie Shatter
  • 185
  • 1
  • 4
  • 14

2 Answers2

6

If you use the C++11 connection syntax you can use a lambda with calls testSlot with your string and fret arguments:

connect(&fretBoard[string][fret], &QPushButton::clicked, [this, string, fret]() {
    testSlot(string, fret);
});

This code creates a lambda using the [captures, ...](arguments, ...) { code } syntax. When you make the connection it captures the string and fret variable values, and will then pass them on to testSlot when the button is clicked.

ajshort
  • 3,684
  • 5
  • 29
  • 43
  • 1
    Just to make it clear, you need Qt 5 and C++11 for this new syntax. – CJCombrink Jan 08 '16 at 05:44
  • I added your line of code. I get an error stating "'this' was not captured for this lambda function". I never worked with lambdas so I am a bit lost. Some more info: String and fret are iterating variables within a nested for loop. The functions header is void MusicScales::testSlot(int s, int f). On GitHub: [link](https://github.com/rshatz/tab.git) musicscales.cpp line 149 – Ritchie Shatter Jan 08 '16 at 17:39
  • I've edited my answer to capture `this`, so you can call member functions. – ajshort Jan 09 '16 at 00:33
  • That did it. Thanks. – Ritchie Shatter Jan 10 '16 at 05:21
  • I'm trying to implement your suggestion as follows: `connect(viewButton[currentAzon], &QPushButton::clicked, [this, currentAzon]() { tableModel->loginSlot(currentAzon); });` But I get an error: ` error: no match for 'operator[]' (operand types are 'QPushButton*' and 'QString')` Even though I am using Qt 5 and C++11. What am I missing? – lte__ May 03 '16 at 12:38
0

There are Two approaches you could use to add the string and fret information. one is to use the sender() function to get the button which emitted the signal. you can the access fret and string if they are members of your button class so in the SLOT you would have.

MyPushButton *button = (MyPushButton *)sender();
button.getFret();
button.getString(); 

However since you are already subClassing QPushButton you could use a private SLOT to catch the buttonClicked signal and re-emit a signal with the right values.

In the constructor

connect(this, SIGNAL(clicked()), this, SLOT(reemitClicked()));

and then the reemit SLOT

void MyPushButton::reemitClicked()
{
    emit clicked(m_fret, m_string);
}

be sure to add the appropriate private slot and public signal to you class https://doc.qt.io/archives/qq/qq10-signalmapper.html see this artical for a good discussion on various ways to add an argument to signal.