如何实现从GUI,而不是编程方式编辑行一个QTableWidget的的垂直和水平标题文本的能力吗?(

2019-10-22 07:38发布

我有被显示在用户界面中,我可以添加和使用按钮删除行和列的QTableWidget的。 问题是,当我添加的行或列,我可以改变数据在实际的细胞,但我不能说出的行或列。 这个名字只是一个静态的数字。

有没有办法让我的程序的用户或许对行/列标题双击和在线或类似的东西编辑名称?

谢谢。

Answer 1:

据我知道有没有内置的方式做到这一点。 然而,这可以手动执行。 下面的代码的主要思想是检测头的项目,地方QLineEdit的在他们双击并保存编辑的文本,一旦失去焦点。 该示例基于Qt生成设计Form类与命名表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标志,不能使用委托。



文章来源: How do I implement the ability to edit a QTableWidget's vertical and horizontal header text in-line from the GUI instead of programmatically?