如何将文本添加到 qtablewidget 的单元格
How to add text to a cell of a tablewidget
我的应用程序中有一个表格小部件。它有 2 列,包括文件名和文件路径。我想用一个按钮向这个 tablewidget 添加文本,并使用这个文本来做一些操作。这该怎么做?
您不能将文本 (QString
) 直接添加到 QTableWidget
,您必须先创建一个 QTableWidgetItem
,然后将其插入所需的单元格。示例:
// create a new qtablewidget
QTableWidget tablewidget;
// our tablewidget has 2 columns
tablewidget.setColumnCount(2);
// we insert a row at position 0 because the tablewidget is still empty
tablewidget.insertRow(0);
// we add the items to the row we added
tablewidget.setItem(0, 0, new QTableWidgetItem("first row, first column"));
tablewidget.setItem(0, 1, new QTableWidgetItem("first row, second column"));
如果你有更多的列,方法是一样的
如果你想添加更多行(即使是第一行也很好),你可以使用通用的
tablewidget.insertRow(tablewidget.rowCount());
总是在 table 的末尾添加一个新行(追加);
更难:这应该可以解释 insertRow()
与不存在的 appendRow()
有何不同(和强大)
QTableWidget tablewidget;
tablewidget.setColumnCount(2);
// we insert a row at position 0 because the tablewidget is still empty
tablewidget.insertRow(tablewidget.rowCount());
tablewidget.setItem(0, 0, new QTableWidgetItem("first row, first column"));
tablewidget.setItem(0, 1, new QTableWidgetItem("first row, second column"));
// "append" new row at the bottom with rowCount()
tablewidget.insertRow(tablewidget.rowCount());
tablewidget.setItem(1, 0, new QTableWidgetItem("second row, first column"));
tablewidget.setItem(1, 1, new QTableWidgetItem("second row, second column"));
// we insert a "new" second row pushing the old row down
tablewidget.insertRow(1);
tablewidget.setItem(1, 0, new QTableWidgetItem("this push the"));
tablewidget.setItem(1, 1, new QTableWidgetItem("second row down"));
我的应用程序中有一个表格小部件。它有 2 列,包括文件名和文件路径。我想用一个按钮向这个 tablewidget 添加文本,并使用这个文本来做一些操作。这该怎么做?
您不能将文本 (QString
) 直接添加到 QTableWidget
,您必须先创建一个 QTableWidgetItem
,然后将其插入所需的单元格。示例:
// create a new qtablewidget
QTableWidget tablewidget;
// our tablewidget has 2 columns
tablewidget.setColumnCount(2);
// we insert a row at position 0 because the tablewidget is still empty
tablewidget.insertRow(0);
// we add the items to the row we added
tablewidget.setItem(0, 0, new QTableWidgetItem("first row, first column"));
tablewidget.setItem(0, 1, new QTableWidgetItem("first row, second column"));
如果你有更多的列,方法是一样的
如果你想添加更多行(即使是第一行也很好),你可以使用通用的
tablewidget.insertRow(tablewidget.rowCount());
总是在 table 的末尾添加一个新行(追加);
更难:这应该可以解释 insertRow()
与不存在的 appendRow()
QTableWidget tablewidget;
tablewidget.setColumnCount(2);
// we insert a row at position 0 because the tablewidget is still empty
tablewidget.insertRow(tablewidget.rowCount());
tablewidget.setItem(0, 0, new QTableWidgetItem("first row, first column"));
tablewidget.setItem(0, 1, new QTableWidgetItem("first row, second column"));
// "append" new row at the bottom with rowCount()
tablewidget.insertRow(tablewidget.rowCount());
tablewidget.setItem(1, 0, new QTableWidgetItem("second row, first column"));
tablewidget.setItem(1, 1, new QTableWidgetItem("second row, second column"));
// we insert a "new" second row pushing the old row down
tablewidget.insertRow(1);
tablewidget.setItem(1, 0, new QTableWidgetItem("this push the"));
tablewidget.setItem(1, 1, new QTableWidgetItem("second row down"));