我正在使用 Qt Creator 和 C++ 开发适用于 PC 和 Android 的应用程序。对于我的应用程序的一部分,我有一个 QTableWidget,用户可以在其中添加条目并编辑框内的信息。
在我用来测试应用程序如何在 Android 上运行的 PC 和 AVD 上,我可以毫无问题地编辑表格的框。然而,现在我已经将实际的 Android 手机连接到我的电脑并在那里测试了应用程序,我无法选择这些框。无论我点击哪里或施加多大压力,这些框都不会选择。
这是应用程序的输出。值得注意的是,“具有空描述的 AccessibilityEvent”行似乎仅在我点击表格中的空框时才会发生。
I e.ChickenChart: Late-enabling -Xcheck:jni
E e.ChickenChart: Unknown bits set in runtime_flags: 0x8000
W System : ClassLoader referenced unknown path:
W e.ChickenChart: Accessing hidden method Landroid/content/ContextWrapper;-\>getDisplay()Landroid/view/Display; (greylist, linking, allowed)
I QtCore : Start
I Qt : qt started
I AdrenoGLES: QUALCOMM build : 961b24f, Ib57168459a
I AdrenoGLES: Build Date : 02/24/20
I AdrenoGLES: OpenGL ES Shader Compiler Version: EV031.27.05.06
I AdrenoGLES: Local Branch :
I AdrenoGLES: Remote Branch :
I AdrenoGLES: Remote Branch :
I AdrenoGLES: Reconstruct Branch :
I AdrenoGLES: Build Config : S L 8.0.12 AArch32
I AdrenoGLES: PFP: 0x005ff113, ME: 0x005ff066
W Gralloc3: mapper 3.x is not supported
W Qt A11Y : Could not (yet) activate platform accessibility.
W Qt A11Y : AccessibilityEvent with empty description
I chatty : uid=10397(org.qtproject.example.ChickenCharts) identical 1 line
W Qt A11Y : AccessibilityEvent with empty description
W Qt A11Y : AccessibilityEvent with empty description
W Qt A11Y : AccessibilityNodeInfo with empty contentDescription: -2147483627
W Qt A11Y : AccessibilityEvent with empty description
W Qt A11Y : AccessibilityEvent with empty description
W Qt A11Y : AccessibilityNodeInfo with empty contentDescription: -2147483565
W Qt A11Y : AccessibilityEvent with empty description
W Qt A11Y : AccessibilityEvent with empty description
W Qt A11Y : AccessibilityNodeInfo with empty contentDescription: -2147483566
W Qt A11Y : AccessibilityEvent with empty description
13:05:31:
"org.qtproject.example.ChickenCharts" died.
我尝试摆脱附加到 QTableWidget 的 QScroller,认为 QScroller 可能以某种方式阻止了对表本身的输入。然而,这并没有解决任何问题。
我也尝试过更改表格的选择模式,但这又不起作用。我不知道问题是什么。
(更新):
正如 hyde 所建议的,我创建了一个简单的应用程序,其中仅包含一个带有 QTableWidget 的窗口。它完全包含在
main()
中,并且表格中只有一个单元格。
我在桌面和手机上都进行了测试。在桌面上,单元格可以正常编辑,但在 Android 上,虽然当我点击单元格时单元格会突出显示,但它不允许我编辑它。
我尝试在这个小型测试应用程序中解决该问题,但到目前为止我还无法解决。我还检查了是否为单元格设置了 ItemIsEditable 标志,确实如此。如果这个问题仍然存在,我可能会考虑更改我的图表以使用 QML。
这是一个可在 Qt Creator 中使用的最小可重现示例:
#include <QLayout>
#include <QTableWidget>
#include <QVBoxLayout>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *window = new QWidget();
QVBoxLayout *v_layout = new QVBoxLayout(window);
QTableWidget *table = new QTableWidget();
table->setRowCount(1);
table->setColumnCount(1);
v_layout->addWidget(table);
window->show();
return a.exec();
}
我找到了解决问题的方法,尽管它存在一些问题。这是:
为了检查表中的单元格是否确实获得了任何输入,我将表的
cellClicked()
信号连接到我自己的函数 onTableCellClicked()
。经过测试,我发现单元格能够接收输入,并且当我单击它时,我可以使用这个新函数来执行代码。
然后我对其进行编码,以便当单击单元格时,在与单元格位置相对应的项目上调用表格的
editItem()
函数。这会强制表格显示编辑器。
我已经对此进行了测试,现在我可以在我的小测试程序的 QTableWidget 中编辑单元格。不过,这有点挑剔。单击某个单元格一次将显示打字栏,但您必须再次单击它才能调出手机键盘。另外,在 PC 上运行应用程序时必须禁用此代码,因为 PC 版本在没有它的情况下已经可以正常工作。
虽然这在技术上解决了我的问题,但我还是要研究 QML。我可能最终会使用它,因为这个解决方案有一些缺陷。
这是代码,以防有人感兴趣:
#include <QLayout>
#include <QTableWidget>
#include <QVBoxLayout>
#include <QApplication>
void onTableCellClicked(int row, int column);
/* changed table to a global variable so that onTableCellClicked() can access it.
If this were the .cpp file of a QDialog, you could just use the 'ui' pointer */
QTableWidget *table;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *window = new QWidget();
QVBoxLayout *v_layout = new QVBoxLayout(window);
table = new QTableWidget(window);
// now we can manipulate the table when a cell is clicked
table->connect(table, &QTableWidget::cellClicked, &onTableCellClicked);
table->setRowCount(3);
table->setColumnCount(3);
v_layout->addWidget(table);
window->show();
return a.exec();
}
void onTableCellClicked(int row, int column)
{
// we can't edit an item in the table if one hasn't been made yet, so we have to check
if (table->item(row, column) == nullptr)
{
table->setItem(row, column, new QTableWidgetItem(""));
}
// here we manually force the table to let us edit the item
table->editItem(table->item(row, column));
}