QPushButtons가 셀 위젯으로있는 QTableWidget이 있습니다. 이 누름 버튼을 테이블에서 행을 삭제하는 수단으로 사용합니다. QPushButtons가 실수로 셀의 중앙에서 이동할 수있는 문제가 있습니다. 아래 이미지는 무슨 일이 일어나고 있는지 보여줍니다.
이는 사용자가 1) 테이블의 마지막 행에서 셀을 선택하고, 2) 해당 셀에 삭제 버튼이 포함되어 있으며, 3) 마지막 행이 부분적으로 보이도록 세로 스크롤 막대가 이동 한 특정 경우에 발생합니다. .
아래는이 문제를 일으키는 아주 최소한의 예입니다. 사용자는 스크롤바를 사용하고 한 틱 위로 스크롤 한 다음 마지막 삭제 버튼을 눌러야합니다.
mainwindow.h :
#pragma once
#include <QMainWindow>
class QSignalMapper;
class QTableWidget;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void DeletePressed(int row);
private:
QSignalMapper* signalMapper_;
QTableWidget* table_;
};
mainwindow.cpp :
#include "mainwindow.h"
#include <QGridLayout>
#include <QSignalMapper>
#include <QTableWidget>
#include <QPushButton>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
signalMapper_(new QSignalMapper(this)),
table_(new QTableWidget())
{
setCentralWidget( new QWidget );
this->setFixedSize(200,300);
QGridLayout *layout = new QGridLayout(centralWidget());
layout->addWidget(table_);
table_->setRowCount(0);
table_->setColumnCount(1);
// if you comment out the following line then the problem is not present
table_->setSelectionMode(QAbstractItemView::SingleSelection);
for (int i = 0; i < 20; i++) {
table_->insertRow(i);
QPushButton *button = new QPushButton("Delete");
connect(button, SIGNAL(clicked(bool)), signalMapper_, SLOT(map()));
signalMapper_->setMapping(button, i);
table_->setCellWidget(i, 0, button);
}
connect(signalMapper_, SIGNAL(mapped(int)), this, SLOT(DeletePressed(int)));
table_->setCurrentCell(19,0);
}
MainWindow::~MainWindow()
{
}
void MainWindow::DeletePressed(int row)
{
table_->clearSelection(); // <- added clear selection before remove row
table_->removeRow(row);
}
main.cpp :
#include <QApplication>
#include "mainwindow.h"
int main( int argc, char* argv[] )
{
QApplication app(argc, argv);
MainWindow wnd;
wnd.show();
return app.exec();
}
문제는 새로운 선택이 이루어질 때 선택한 셀을 계속 표시하려는 QTableWidget에서 비롯된 것이라고 생각합니다. 여기에 게시 된 두 가지 답변을 모두 시도했지만 내 문제가 해결되지 않았습니다.
누구든지이 문제를 해결하는 방법을 알려 주시면 도움을 주시면 감사하겠습니다.
나는 오래 전에 비슷한 문제 QtreeWidget
가 있었고 callig 보호 방법으로 해결했습니다 updateGeometries()
.
특수화하지 않은 QTableView
경우이 함수를 단순히 호출 할 수 없습니다 (보호되어 있으므로). 여기에 간단하게 수행하는 트릭이 있습니다.
class MyQTableView : public QTableView
{
public:
inline void publicUpdateGeometries() { updateGeometries(); }
};
void MainWindow::DeletePressed(int row)
{
table_->clearSelection(); // <- added clear selection before remove row
table_->removeRow(row);
// call the protected method QTableView::updateGeometries()
((MyQTableView*) table_)->publicUpdateGeometries();
}
이것이 문제를 해결할 수 있기를 바랍니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다