如何实现从GUI而不是以编程方式在线编辑QTableWidget的垂直和水平标题文本的功能?

时间:2015-03-19 14:24:42

标签: qt qt5 qtablewidget

我有一个QTableWidget,显示在用户界面中,我可以使用按钮添加和删除行和列。问题是,当我添加行或列时,我可以更改实际单元格中的数据,但我无法命名行或列。该名称只是一个静态数字。

有没有办法让我的程序用户可能双击行/列标题并在线编辑名称或类似名称?

感谢。

1 个答案:

答案 0 :(得分:2)

据我所知,没有内置方法可以做到这一点。但是,这可以手动实现。以下代码的主要思想是检测标题项的双击,将QLineEdit放在它们上,并在失去焦点后保存已编辑的文本。该示例基于Qt生成的Designer Form Class,其中包含一个名为ui->tableWidget的表,可以是QTableWidgetQTableView

class MainWindow : public QMainWindow {
  Q_OBJECT
public:
  explicit MainWindow(QWidget *parent = 0);
  ~MainWindow();
private:
  Ui::MainWindow *ui;
  QLineEdit* header_editor;
  int editor_index;
  bool eventFilter(QObject*, QEvent*);
};

来源:

MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::MainWindow)
{
  header_editor = 0;
  ui->setupUi(this);
  ui->tableWidget->horizontalHeader()->viewport()->installEventFilter(this);
  ui->tableWidget->verticalHeader()->viewport()->installEventFilter(this);
}

MainWindow::~MainWindow() {
  delete ui;
}

bool MainWindow::eventFilter(QObject* object, QEvent* event) {
  if ((object == ui->tableWidget->horizontalHeader()->viewport() ||
       object == ui->tableWidget->verticalHeader()->viewport()) &&
      event->type() == QEvent::MouseButtonDblClick) {
    if (header_editor) { //delete previous editor just in case
      header_editor->deleteLater();
      header_editor = 0;
    }
    QMouseEvent* e = static_cast<QMouseEvent*>(event);
    QHeaderView* header = static_cast<QHeaderView*>(object->parent());
    int mouse_pos = header->orientation() == Qt::Horizontal ? e->x() : e->y();
    int logical_index = header->logicalIndex(header->visualIndexAt(mouse_pos));
    if (logical_index >= 0) { // if mouse is over an item
      QRect rect; // line edit rect in header's viewport's coordinates
      if (header->orientation() == Qt::Horizontal) {
        rect.setLeft(header->sectionPosition(logical_index));
        rect.setWidth(header->sectionSize(logical_index));
        rect.setTop(0);
        rect.setHeight(header->height());
      } else {
        rect.setTop(header->sectionPosition(logical_index));
        rect.setHeight(header->sectionSize(logical_index));
        rect.setLeft(0);
        rect.setWidth(header->width());
      }
      rect.adjust(1, 1, -1, -1);
      header_editor = new QLineEdit(header->viewport());
      header_editor->move(rect.topLeft());
      header_editor->resize(rect.size());
      header_editor->setFrame(false);
      //get current item text
      QString text = header->model()->
          headerData(logical_index, header->orientation()).toString();
      header_editor->setText(text);
      header_editor->setFocus();
      editor_index = logical_index; //save for future use
      header_editor->installEventFilter(this); //catch focus out event
      //if user presses Enter it should close editor
      connect(header_editor, SIGNAL(returnPressed()), 
              ui->tableWidget, SLOT(setFocus()));
      header_editor->show();
    }
    return true; // filter out event
  } else if (object == header_editor && event->type() == QEvent::FocusOut) {
    QHeaderView* header = static_cast<QHeaderView*>(
        header_editor->parentWidget()->parentWidget());
    //save item text
    header->model()->setHeaderData(editor_index, header->orientation(), 
                                   header_editor->text());
    header_editor->deleteLater(); //safely delete editor
    header_editor = 0;
  }
  return false;
}

这种方法的缺点是它很糟糕,当标题调整大小或滚动表格时会出现问题。这只是一个可以改进的例子。

我觉得必须有一种更简单的方法。但Qt标头忽略Qt::ItemIsEditable标志,不能使用委托。