Question

Im a student developer using Qt to build a GUI to help users plot specific columns of data located in multiple files. The feature I'm setting up allows users to select a file using a button in each row. So the button originally would say browse and then user clicks it to open a dialog to select a file then the button text is replaced with the file name selected. Sorry for the story; my simple attempt to add some clarity.

The problem I'm having is I'm not sure how to set a policy up for the button clicked. I'd imagine that I'd have to extend the functionality of each of the QPushButtons but I don't really know how to do that. So far I am using the following to set the cell widget.

//with row count set dimensions are set becasue column count is static
    //begin bulding custom widgets/QTableWidgetItems into cells
    for(int x = 0; x < ui->tableWidgetPlotLineList->rowCount(); x++)
    {
        for(int y = 0; y < ui->tableWidgetPlotLineList->columnCount(); y++)
        {
            if(y == 1)
            {
                //install button widget for file selection
                QPushButton *fileButton = new QPushButton();
                if(setDataStruct.plotLineListData.at(rowCount).lineFileName != "");
                {
                    fileButton->setText(setDataStruct.plotLineListData.at(rowCount).lineFileName);
                }
                else
                {
                    fileButton->setText("Browse...");
                }
                ui->tableWidgetPlotLineList->setCellWidget(x, y, fileButton);
            }

I was thinking that

connect(ui->tableWidgetPlotLineList->row(x), SIGNAL(fileButton->clicked()), this, SLOT(selectPlotLineFile(x));

might do the trick but I think I'm probably going in the wrong direction here. Honestly I'm not even too sure as to where it would go...

Thanks so much for reading my post. Please let me know if there is anything lacking from this post and I will update it immediately. I'd also like to thank any contributions to this post in advance!

Was it helpful?

Solution

connect(ui->tableWidgetPlotLineList->row(x), SIGNAL(fileButton->clicked()), this, SLOT(selectPlotLineFile(x));

Is not syntactically correct for a signal/slot connection. Something like this would be more appropriate:

connect(fileButton, SIGNAL(clicked()), this, SLOT(selectPlotLineFile(x));

...

If you need access to the specific button that emited the clicked() signal than you could use the sender() function in your slot:

void selectPlotLineFile() {
    QPushButton *button = dynamic_cast<QPushButton*>( sender() )
}

Now you may be wondering how you know which row to operate on. There are several different approaches, one of the easier ones being to maintain a QMap<QPushButton*, int> member variable that you can use to lookup which button belongs to which row.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top