我是一个学生开发人员,使用qt构建GUI,以帮助用户绘制位于多个文件中的特定数据列。我设置的功能允许用户使用每行中的按钮选择文件。所以按钮最初会说浏览,然后用户单击它以打开一个对话框以选择文件,然后将按钮文本替换为所选的文件名。抱歉这个故事;我简单地尝试添加一些清晰度。

我所拥有的问题是我不确定如何为单击的按钮设置策略。我想我必须扩展每个QpushButtons的功能,但我真的不知道该怎么做。到目前为止,我正在使用以下内容来设置单元格窗口小部件。

//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);
            }
.

我在想

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

可能会做这个伎俩,但我觉得我可能在这里遇到错误的方向。老实说,我甚至不太确定它会去哪里......

非常感谢阅读我的帖子。请告诉我是否有缺少这篇文章的任何东西,我会立即更新它。我还要注意提前对此帖子的任何贡献!

有帮助吗?

解决方案

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

对信号/插槽连接没有句子校正。这样的东西更加合适:

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

...

如果您需要访问生成生成的特定按钮,则会使用比您在插槽中使用的emit函数:

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

现在您可能想知道您如何知道哪一行运行。有几种不同的方法,一个更容易的方法是维护一个可以用于查找哪个按钮属于哪一行的clicked()成员变量之一。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top