0

PART A:

I have created a widget called Panel which I'd like to iteratively make new instances of.

so, for example, it would look something like:

 Panel *panelArray[10];
 for(int i=0;i<10;i++) panelArray[i] = new Panel(this);

would this be the appropriate syntax?

PART B:

If so, how am I to manually hook up signals emitted from each of the panels?

Example:

 for(int i=0;i<10,i++) connect(panelArray[i], SIGNAL(raiseToggleGUICmd(QByteArray)), this, SLOT(writeData(QByteArray)));

Thanks in advance!

Rachael
  • 1,965
  • 4
  • 29
  • 55

1 Answers1

2

Part A looks normal.

Part B looks normal too, but if you want to know which widget emit signal, you should use something like this( in your case, your slot do same thing with every widget)

Usage of QSignalMapper

signalMapper = new QSignalMapper(this);
for (int i = 0; i < 3; ++i)
   {
       QPushButton *button = new QPushButton(QString::number(i),this);
       connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
       button->move(i*10,i*10);//doesn't matter

       signalMapper->setMapping(button, QString::number(i));
   }
connect(signalMapper, SIGNAL(mapped(const QString &)),
            this, SLOT(clicked(const QString &)));

//...
void MainWindow::clicked(const QString & text)
{
    QMessageBox::information(this, "TEST", text, QMessageBox::Ok);
}

Or using sender()

for (int i = 0; i < 3; ++i)
   {
       QPushButton *button = new QPushButton(QString::number(i),this);
       button->setObjectName(QString::number(i));//important
       connect(button, SIGNAL(clicked()), this, SLOT(clicked()));
       button->move(i*10,i*10);
   }

void MainWindow::clicked()
{
    switch( sender()->objectName().toInt())
    {
        case 0:
        QMessageBox::information(this, "TEST", "0", QMessageBox::Ok);//do something specific to 0 widget
        break;
    case 1:
    QMessageBox::information(this, "TEST", "1", QMessageBox::Ok);//do something specific to 1 widget
    break;
    case 2:
    QMessageBox::information(this, "TEST", "2", QMessageBox::Ok);//and so on
    break;
    }
}
Jablonski
  • 18,083
  • 2
  • 46
  • 47
  • Wow. I was wondering about this and had seen some signalmapper stuff come up in my google search...big thank you for this awesome post! I will definitely be using this in the future, I'm sure! Thanks again. This is wonderful. Please add a little blerb at the top of your answer acknowledging/commenting on the components of my question directly and I'll check this as "answered" – Rachael Sep 10 '14 at 16:38
  • @Rachael unfortunately English isn't my native language and I don't know what is this "blerb". Exuse me. See my edit please, is that what you want? – Jablonski Sep 10 '14 at 17:02
  • sorry about that. "Blurb" is the correct spelling and I was using it to ask "please [*write a little something*] at the top of your answer...". Thanks again. – Rachael Sep 10 '14 at 17:26