0

I have created a QPushButton in the last column of tableview (which contains ip address of the connected clients to my application). With that button I am able to disconnect the connected client in that particular row using button release signal and slot 'handlebutton(int)'.

The code is -

MainWindow::MainWindow(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QSortFilterProxyModel *model = new QSortFilterProxyModel(this);
    model = pCApp->guiClient()->getConnectionManagement()->getProxyModel();
    ui->tableView->setModel(model);
    QPushButton *button;
    QSignalMapper *mapper = new QSignalMapper(this);
    QObject::connect(mapper, SIGNAL (mapped(int)), this, SLOT (handleButton(int)));
    for (int i = 0; i < model->rowCount(); i++)
    {
        button = new QPushButton;
        button->setText("Disconnect " + QString::number(i));
        button->setStyleSheet("QPushButton { color: #E5E5E5; }");
        ui->tableView->setIndexWidget(model->index(i,2, QModelIndex()), button);
        QObject::connect(button, SIGNAL(released()), mapper, SLOT(map()));
        mapper->setMapping(button, i);
    }
    setAttribute(Qt::WA_DeleteOnClose);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::handleButton(int row)
{
    CGuiClientMessage message;
    message.setRecipient(CGuiMessage::R_GUISERVER);
    message.setObjectId(0);
    message.setCommand(CGuiMessage::DISCONNECT_PEER);
    message.Parameter().setAttribute("Peers", ui->tableView->model()->data(ui->tableView->model()->index(row,1)).toString());
    pCApp->guiClient()->SendMessageToPts(message);
}

Now, I want to update mapping. Where shall I do that in the slot or somewhere else? Please, if anyone can suggest me how and where to do it?

Thanks in advance!

Louis Langholtz
  • 2,913
  • 3
  • 17
  • 40
tanmayb
  • 13
  • 6

1 Answers1

0

If I got it right, you just want to unmap a button as soon as it was clicked and corresponding client was disconnected. Then you can just call mapper->removeMapping(button) on the appropriate button. If you need to map this button again - call mapper->setMapping(button, i) again.

Keep in mind, it does not disconnect button released signal from the mapper. If you need - use QObject::disconnect explicitely.

Also if your button gets destroyed - both removeMapping and disconnect are done for you so you don't have to worry about that.

WindyFields
  • 2,697
  • 1
  • 18
  • 21
  • Thanks for that quick reply. I have used mapper->setMapping(button, i) in the loop, but it doesn't seem to work. Can you tell where should I place it? – tanmayb Aug 24 '17 at 11:01
  • @tanmayb, it is already a different question. [Here is an example](http://doc.qt.io/qt-5/qsignalmapper.html) how to use `setMapping(button, ...)` in a loop. Just do the same. If it does not goes well, I recommend to start from mapping one single button first and then just add a loop to map few more. Also, in debugging purposes you can make `handleButton` slot to output a simple message to the console. And the last thing I would recommend, is to use [Qt 5 syntax](http://doc.qt.io/qt-5/signalsandslots.html) for signals and slots connections. – WindyFields Aug 24 '17 at 11:16