显示隐藏的 QTableView 列
我正在尝试做一些看起来应该非常简单的事情,但是我越深入地研究它,我想知道这是否是一个 Qt bug。
因此,我有一个 QTableView,其中的列可以根据用户的喜好显示/隐藏。初始化表格后,我调用自定义 restoreColumns()
方法来隐藏用户上次隐藏的列(使用 QTableView::hideColumn()
) GUI 已打开。
当用户尝试显示上次运行 GUI 时隐藏的列时,问题就会出现。适当的信号/槽被调用并运行,但由于某种原因,QTableView
没有更新以显示该列。
奇怪的是,任何已显示的列(上次运行 GUI 时用户未隐藏)都没有隐藏/显示的问题。
有什么想法吗?谢谢!
以下是我初始化表的方法...
m_tableModel = new mytablemodel();
m_tableView = new mytableview();
m_tableView->setItemDelegate(m_tableDelegate);
m_tableView->setModel(m_tableModel);
RestoreColumns() 方法的主要内容:
for (int i=0; i<horizontalHeader()->count(); i++) {
// load size to restore previous width
...
horizontalHeader()->resizeSection(i, width); // restore width
// load previous column position
...
// restore column order
int currentVisualIndex = horizontalHeader()->visualIndex(i);
if (currentVisualIndex != visualIndex)
horizontalHeader()->moveSection(currentVisualIndex, visualIndex);
// load previous hidden/shown state
...
if (columnHidden) {
hideColumn(i);
} else {
showColumn(i);
}
}
下面是一些用于显示/隐藏其中一列的示例代码。
void mytableview::showAColumn(bool checked) {
// mytableview is a subclass of qtableview
if (checked)
showColumn(COLUMN_A); // COLUMN_A is an enum for the column
else
hideColumn(COLUMN_A);
}
它连接到一个 QAction
,可以从 QTableView
的 QHeaderView
的菜单和上下文菜单访问。
connect(action, SIGNAL(toggled(bool)), this, SLOT(showAColumn(bool)));
I'm trying to do something that seems like it should be very simple, but the more I look into it I wonder if it's a Qt bug.
So, I have a QTableView
that has columns that can be shown/hidden as the user likes. After I initialize the table, I call a custom restoreColumns()
method that hides the columns (using QTableView::hideColumn()
) that the user had hidden the last time the GUI was open.
The problem then comes when the user tries to show the columns that were hidden by the user the last time the GUI was ran. The appropriate signal/slot gets called and run through but for some reason the QTableView
isn't updating to display the column.
What's weird is that any column that is already displayed (was not hidden by the user the last time the GUI was ran) has no problems with getting hidden/shown.
Any thoughts? Thanks!
Here's how I initialize the table...
m_tableModel = new mytablemodel();
m_tableView = new mytableview();
m_tableView->setItemDelegate(m_tableDelegate);
m_tableView->setModel(m_tableModel);
Meat of restoreColumns() method:
for (int i=0; i<horizontalHeader()->count(); i++) {
// load size to restore previous width
...
horizontalHeader()->resizeSection(i, width); // restore width
// load previous column position
...
// restore column order
int currentVisualIndex = horizontalHeader()->visualIndex(i);
if (currentVisualIndex != visualIndex)
horizontalHeader()->moveSection(currentVisualIndex, visualIndex);
// load previous hidden/shown state
...
if (columnHidden) {
hideColumn(i);
} else {
showColumn(i);
}
}
Below is some sample code to show/hide one of the columns.
void mytableview::showAColumn(bool checked) {
// mytableview is a subclass of qtableview
if (checked)
showColumn(COLUMN_A); // COLUMN_A is an enum for the column
else
hideColumn(COLUMN_A);
}
Which is connected to a QAction
that can be accessed from the Menu and Context Menu of the QHeaderView
of the QTableView
.
connect(action, SIGNAL(toggled(bool)), this, SLOT(showAColumn(bool)));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您加载隐藏列的先前宽度时,保存的宽度为 0。
因此,调整列大小时请确保宽度大于 0。
执行此操作,然后列将按预期显示/隐藏。
When you are loading the previous width of the hidden columns, the width that was saved was 0.
So, when resizing the column make sure that the width is greater than 0.
Do this and then the columns will show/hide as expected.