1

我试图用谷歌找到这个问题的答案,但没有任何结果。我正在将应用程序从 Qt4 转换为 Qt5。该应用程序在 Qt4 中完美编译,但是当我现在尝试针对 Qt5 进行编译时,它给了我这个权限错误。由于这个类的状态在两个版本中都受到保护,我很难理解我需要改变什么。

这个编译问题已在几个不同的 Ubuntu 安装(包括 wsl)上复制,但我还没有在 Fedora 中尝试过。

这是该类的一个子集

#include <QWidget>
#include <QEvent>
#include <QTableWidget>
#include <QItemDelegate>
#include <QModelIndex>
#include <QSize>
#include <qdialog.h>
#include <qcombobox.h>
#include "ui_pegs_page.h"
#include <string>

class EGS_ConfigReader;
class QProcess;
class PEGS_RunOutput;
class QTableWidget;

struct Element {
  int   Z;
  std::string symbol;
 float  aw;
 float  Iev;
 float  rho;
};

const int n_element = 100;

extern Element element_data[];

class TableEventHandler : public QObject {
    Q_OBJECT
public:
    TableEventHandler(QTableWidget *parent);
protected:
    bool eventFilter(QObject *o, QEvent *e);
private:
    QStringList itemCopy;
    QList<QTableWidgetSelectionRange> copyRange;
};

编辑:

这是有问题的方法。

TableEventHandler::TableEventHandler(QTableWidget *parent) :
  QObject(parent) {
  if( parent == 0 )
   qFatal("TableEventHandler::TableEventHandler: parent can not be null!");
}


bool TableEventHandler::eventFilter(QObject *o, QEvent *e) {
  if( !o ) qWarning("TableEventHandler::eventFilter called with 0 object?");
  if( QString(o->metaObject()->className()) != tr("QTableWidget") ) {
#ifdef EI_DEBUG
      qDebug("Only QTableWidget objects accepted! Returning!");
#endif
      return false;
  }
  QTableWidget *to = (QTableWidget *)o;
  if( e->type() == QEvent::KeyPress ) {
    QKeyEvent *ke = (QKeyEvent*)e;
    if(ke->matches(QKeySequence::Copy) ){
       QString cellText; itemCopy.clear(); copyRange.clear();
       QList<QTableWidgetSelectionRange> ts = to->selectedRanges();
       if(!ts.isEmpty()) {
          for ( int irow = ts.first().topRow(); irow <= ts.first().bottomRow(); irow++){
               for ( int icol = ts.first().leftColumn(); icol <= ts.first().rightColumn(); icol++){
                   QTableWidgetItem *w = to->item(irow,icol);
                   if(w) cellText = w->text();
                   if ( !cellText.isEmpty() ){
                      itemCopy << cellText;
                   }
                   else
                      itemCopy << " ";
               }
          }
          copyRange = ts;
          //cout << itemCopy.join(", ").toLatin1().data() << endl;
       }
       else {
            QTableWidgetItem *w = to->item(to->currentRow(), to->currentColumn());
            if (w) cellText = w->text();
            if ( !cellText.isEmpty() )
                 itemCopy << cellText;
            else itemCopy << "";
       }
       return true;
    }
    else if(ke->matches(QKeySequence::Paste) && !itemCopy.isEmpty() && !copyRange.isEmpty()){
       QList<QTableWidgetSelectionRange> cs = to->selectedRanges();
       int top = cs.first().topRow(), left = cs.first().leftColumn(), icount = 0;
       QTableWidgetSelectionRange ts = QTableWidgetSelectionRange(
                                       top , left,
                                       top  + copyRange.first().rowCount()-1,
                                       left + copyRange.first().columnCount()-1);
       for ( int irow = ts.topRow(); irow <= ts.bottomRow(); irow++){
         for ( int icol = ts.leftColumn(); icol <= ts.rightColumn(); icol++){
             if ( ++icount <= itemCopy.size() )
                to->setItem(irow, icol, new QTableWidgetItem(itemCopy[icount-1]));
                to->setItem(irow, icol, new QTableWidgetItem(itemCopy[icount-1]));
         }
       }
       return true;
    }
    else if(ke->matches(QKeySequence::Cut) ){
       QString cellText; itemCopy.clear(); copyRange.clear();
       QList<QTableWidgetSelectionRange> ts = to->selectedRanges();
       if(!ts.isEmpty()) {
         for (int irow = ts.first().topRow(); irow <= ts.first().bottomRow(); irow++) {
           for(int icol = ts.first().leftColumn(); icol <= ts.first().rightColumn(); icol++) {
               QTableWidgetItem *w = to->item(irow,icol);
               if(w) cellText = w->text();
               if ( !cellText.isEmpty() ){
                  itemCopy << cellText;
               }
               else
                  itemCopy << "";
               to->setItem(irow,icol,0);
           }
         }
         copyRange = ts;
         //cout << itemCopy.join(", ").toLatin1().data() << endl;
       }
       return true;
    }
    else if(ke->matches(QKeySequence::Delete) ){
       QList<QTableWidgetSelectionRange> ts = to->selectedRanges();
       if(!ts.isEmpty()) {
         for (int irow = ts.first().topRow(); irow <= ts.first().bottomRow(); irow++) {
           for(int icol = ts.first().leftColumn(); icol <= ts.first().rightColumn(); icol++) {
               to->setItem(irow,icol,0);
           }
         }
       }
       return true;
    }
    else
        to->eventFilter(o, e);

  }
  return false;
}
4

1 回答 1

0

您正在访问受保护的QAbstractScrollArea::eventFilter(QObject*, QEvent*)方法

  1. 来自不继承自QAbstractScrollArea最有可能)的类的方法,或
  2. QAbstractScrollArea来自不是(不太可能)朋友的其他类的方法,或
  3. 来自某个不是QAbstractScrollArea(不太可能)朋友的函数。

请注意,TableEventHandler直接继承自QObject而不是继承自QAbstractScrollArea. 因此,如果您尝试QAbstractScrollArea::eventFilter(QObject*, QEvent*)从 的方法之一调用TableEventHandler,则会收到该错误。

编辑:查看您编辑的答案,我看到您正在打电话

to->eventFilter(o, e);

TableEventHandler::eventFilter(QObject *o, QEvent *e)哪里QTableWidget *to = (QTableWidget *)o;。程序员可能意味着此时TableEventHandler::eventFilter不要过滤相应的事件。然后,该方法应该只是返回false以将控制权传递给稍后安装在该对象上的任何其他事件过滤器。

于 2017-02-14T21:26:59.357 回答