2017-02-14 31 views
1

我試圖找到這個谷歌的答案,但沒有任何結果。我正在將應用程序從Qt4轉換爲Qt5。這個應用程序在Qt4中完美編譯,但是當我嘗試對Qt5進行編譯時,它給了我這個權限錯誤。由於這個類的狀態在兩個版本中都受到保護,所以我很難理解需要更改的內容。'虛擬布爾QAbstractScrollArea :: eventFilter(QObject *,QEvent *)'被保護

這個編譯問題已被複制到幾個不同的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; 
} 

回答

0

你從一些方法訪問從一個類的方法保護QAbstractScrollArea::eventFilter(QObject*, QEvent*)方法要麼

  1. 不從QAbstractScrollArea繼承(最有可能),或
  2. 其他類別不是QAbstractScrollArea(不太可能)的朋友,或者
  3. 來自不是QAbstractScrollArea(不太可能)的朋友的某個功能。

請注意,TableEventHandler直接繼承QObject而不是從QAbstractScrollArea繼承。因此,如果您試圖從TableEventHandler的某個方法調用QAbstractScrollArea::eventFilter(QObject*, QEvent*),那麼您會得到該錯誤。

編輯:看你的編輯答案,我看到你在哪裏TableEventHandler::eventFilter(QObject *o, QEvent *e)調用QTableWidget *to = (QTableWidget *)o;

to->eventFilter(o, e); 

。程序員可能意味着TableEventHandler::eventFilter不會過濾相應的事件。然後該方法應該返回false將控制權傳遞給稍後安裝在該對象上的任何其他事件過濾器。

+0

我看到你要去那裏,並將探索。我想知道,如果這是繼承問題,這是不是在QT4? – crcrewso