0

Source Code
http://www.informit.com/store/c-plus-plus-gui-programming-with-qt4-9780132354165
PDF File
https://www.researchgate.net/publication/215458943_C_GUI_programming_with_Qt_4

引き続きお願いします。

mainwindow.cppファイルで余計なメンバ関数は省いています。
下記のエラーが発生します。

1.lineEdit is a private member of 'GoToCellDialog'
2.variable has imcomplete type SpreadSheetCompare
3.no member named "name" in SortDialog

SortDialogGoToCellDialogが関わっているようなのですが、どこがどうなっているのでしょうか。SortDialogについてはC++ GUI Programming with Qt 4 UIのエラー直前の質問でコードを載せてますので、申し訳ありませんがそちらをご覧ください。GoToCellDialogはここに載せます。(過去に掲載はしましたが不完全なので。)

mainwindow.cpp

#include "mainwindow.h"
#include "gotocelldialog.h"
#include "sortdialog.h"
#include "spreadsheet.h"
#include "finddialog.h"
#include "ui_gotocelldialog.h"
#include "ui_sortdialog.h"
#include <QtWidgets>
#include <QtGui>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{   spreadsheet = new Spreadsheet;
    setCentralWidget(spreadsheet);

    createActions();
    createMenus();
    createContextMenu();
    createToolBars();
    createStatusBar();
    readSettings();
    findDialog = nullptr;
    setWindowIcon(QIcon(":/images/icon.png"));
    setCurrentFile("");


}

省略~



void MainWindow::goToCell()
{
            GoToCellDialog *dialog = new GoToCellDialog(this);
            if (dialog->exec()){
                QString str = dialog->lineEdit->text().toUpper();#here lineEdit is a private member of 'GoToCellDialog'
                spreadsheet->setCurrentCell(str.mid(1).toInt() -1,
                                            str[0].unicode() - 'A');
            }
        }
void MainWindow::sort()
{
            SortDialog *dialog = new SortDialog(this);
            QTableWidgetSelectionRange range = spreadsheet->selectedRange();
            dialog->setColumnRange('A' + range.leftColumn(),
                                  'A' + range.rightColumn());
            if (dialog.exec()){
                SpreadSheetCompare compare;\\unknown type name "SpreadSheetCompare"
                compare.keys[0] =
                dialog->primaryColumnCombo->currentIndex() - 1;//here no member named "primaryColumnCombo" in SortDialog
                compare.keys[1] =
                dialog.secondaryColumnCombo->currentIndex() - 1;//here also
                compare.ascending[0] =
                (dialog.primaryOrderCombo->currentIndex() == 0);//here also
                compare.ascending[1] =
                (dialog.secondaryOrderCombo->currentIndex() == 0);//here also
                compare.ascending[2] =
                (dialog.tertiaryOrderCombo->currentIndex() == 0);//here  also
                spreadsheet->sort(compare);
            }
        }



MainWindow::~MainWindow()
{

}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class QAction;
class QLabel;
class FindDialog;
class SortDialog;
class GoToCellDialog;
class SpreadSheetCompare;
class Spreadsheet;

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
protected:
    void closeEvent(QCloseEvent *event);
private slots:
    void newFile();
    void open();
    bool save();
    bool saveAs();
    void find();
    void goToCell();
    void sort();
    void about();
    void openRecentFile();
    void updateStatusBar();
    void spreadsheetModified();
private:
    void createActions();
    void createMenus();
    void createContextMenu();
    void createToolBars();
    void createStatusBar();
    void readSettings();
    void writeSettings();
    bool okToContinue();
    bool loadFile(const QString &fileName);
    bool saveFile(const QString &fileName);
    void setCurrentFile(const QString &fileName);
    void updateRecentFileActions();
    QString strippedName(const QString &fullFileName);

    Spreadsheet *spreadsheet;
    FindDialog *findDialog;
    QLabel *locationLabel;
    QLabel *formulaLabel;
    QStringList recentFiles;
    QString curFile;
    enum {MaxRecentFiles = 5};
    QAction *recentFileActions[MaxRecentFiles];
    QAction *separatorAction;

    QMenu *fileMenu;
    QMenu *editMenu;
    QMenu *selectSubMenu;
    QMenu *toolsMenu;
    QMenu *optionsMenu;
    QMenu *helpMenu;
    QToolBar *fileToolBar;
    QToolBar *editToolBar;
    QAction *newAction;
    QAction *openAction;
    QAction *exitAction;
    QAction *selectAllAction;
    QAction *showGridAction;
    QAction *aboutAction;
    QAction *aboutQtAction;
    QAction *saveAction;
    QAction *saveAsAction;
    QAction *cutAction;
    QAction *copyAction;
    QAction *deleteAction;
    QAction *pasteAction;
    QAction *findAction;
    QAction *goToCellAction;
    QAction *selectRowAction;
    QAction *selectColumnAction;
    QAction *recalculateAction;
    QAction *sortAction;
    QAction *autoRecalcAction;



};


#endif // MAINWINDOW_H

gotocelldialog.h

#ifndef GOTOCELLDIALOG_H
#define GOTOCELLDIALOG_H
#include <QDialog>
#include <QLineEdit>


#include <QtUiTools/QUiLoader>
namespace Ui {
class GoToCellDialog;
}
class GoToCellDialog : public QDialog
{
    Q_OBJECT
public:
    GoToCellDialog(QWidget *parent=nullptr);
private slots:
    void on_lineEdit_textChanged();
private:
    QPushButton *okButton;
    QPushButton *cancelButton;
    QLineEdit *lineEdit;
    Ui::GoToCellDialog *ui;
    };

#endif // GOTOCELLDIALOG_H

gotocelldialog.cpp

#include <QtGui>
#include <QtWidgets>
#include <QtCore>
#include <QRegExpValidator>
#include "gotocelldialog.h"
#include "ui_gotocelldialog.h"

GoToCellDialog::GoToCellDialog(QWidget *parent)
    :QDialog(parent)

{
    ui->setupUi(this);

    QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");
    ui->lineEdit->setValidator(new QRegExpValidator(regExp,this));

    connect(okButton,SIGNAL(clicked()),this,SLOT(accept()));
    connect(cancelButton,SIGNAL(clicked()),this,SLOT(reject()));
}
void GoToCellDialog::on_lineEdit_textChanged()
{
    okButton->setEnabled(ui->lineEdit->hasAcceptableInput());
}

gotocelldialog.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>GoToCellDialog</class>
 <widget class="QDialog" name="GoToCellDialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>384</width>
    <height>208</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Go to Cell</string>
  </property>
  <widget class="QWidget" name="">
   <property name="geometry">
    <rect>
     <x>60</x>
     <y>70</y>
     <width>249</width>
     <height>62</height>
    </rect>
   </property>
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <layout class="QHBoxLayout" name="horizontalLayout">
      <item>
       <widget class="QLabel" name="label">
        <property name="text">
         <string>&amp;Cell Location</string>
        </property>
        <property name="buddy">
         <cstring>lineEdit</cstring>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QLineEdit" name="lineEdit"/>
      </item>
     </layout>
    </item>
    <item>
     <layout class="QHBoxLayout" name="horizontalLayout_2">
      <item>
       <spacer name="horizontalSpacer">
        <property name="orientation">
         <enum>Qt::Horizontal</enum>
        </property>
        <property name="sizeHint" stdset="0">
         <size>
          <width>40</width>
          <height>20</height>
         </size>
        </property>
       </spacer>
      </item>
      <item>
       <widget class="QPushButton" name="okButton">
        <property name="enabled">
         <bool>false</bool>
        </property>
        <property name="text">
         <string>OK</string>
        </property>
        <property name="default">
         <bool>true</bool>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QPushButton" name="cancelButton">
        <property name="text">
         <string>Cancel</string>
        </property>
       </widget>
      </item>
     </layout>
    </item>
   </layout>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

実行環境
qtcreator 4.7
qt 5.9.6
c++11
コンパイラ:

Desktop_Qt_5_9_6_MSVC2017_64bit

5
  • 1
    先ほどの質問のコメントに書くべきだったのですが、ご質問に掲載されたコードはpdfの書籍内に掲載されたコードともDownload可能なサンプルコード(両者はほぼ一致しているはず)とも、根本的に異なるクラス構造に変更してしまっているようです。(参考コードは多重継承を使っているのに、御掲載のコードは単独継承。)一般にこのような書き換えをして完全に元と同じ動作をさせるのは、かなりの困難を伴います。このご質問に掲載されたエラーを場当たり的に解決して、ビルド時のエラーは出なくなっても、今度は実行時エラーに悩まされることになるでしょう。このような書き換えそのものが目的なのでしたら、別に構わない(相当の苦労はするでしょうが、良い経験にはなると思います)のですが、もし他に理由があったのであれば、一度立ち止まってやり方を見直した方がいいように思われるのですが?
    – OOPer
    2018年9月15日 8:58
  • @OOPer 「根本的に異なる構造」ですか。PyQtの本もPythonなのに苦労した経験があります。同じようにバージョン等が異なる事が原因ですね。PySideで始めたのもあります。確かに、もう本とかに頼らずリファレンスを見ながら一から構築していったように思います。(最初はファイルの分割さえできなかったです。)そういう風にした方が確かにいいかもしれませんね。うすうす感づいてはいたのですが、もしそのままを作るなら本の状態と同じものを利用するようにした方がいいですね。今回の件が落ち着いたら方針を切り替えるように考えます。(今回だけは粘ります。) 2018年9月15日 10:48
  • PyQtの本は結構読みこんだのですが、C++Qtはまだ真っ白のままでしたので、pythonを一生懸命やっている内に時代遅れになってしまったようですね。Qt5の書籍や、アイデア辞書本として使い、もっと詳しくなったらコードの改変をするべきですね。ありがとうございました。 2018年9月15日 10:55
  • せっかくこれまで色々と苦労をしてこられているのに水を差すようなことを言ってしまい申し訳ありません。ただ、時代遅れとか言う問題ではないように思います。ご参照の書籍では多重継承を用いているのに、現在のコードは継承を用いない方法(日本語の解説サイトにはこちらの方が多いようです)で書こうとされています。作法が違うものを混ぜこぜにしてしまっていると言うことですね。どちらの作法を学習するのか決められて、あとは同じ作法(と言っても学習途中ではわかりにくいでしょうが)の情報源を見つけて、やり方を統一しないといけないかと思います。(作法が2通りあってどちらかにしないといけないと言う意味の話は過去質問の回答に書いてあります。soramimiさんのアドバイス「最近は、集約(uiポインタを使う方)が主流」に従うなら、現在の参考書籍は「主流」じゃない作法ということになります)今やっている混ぜこぜは、C++についても学習中という方にはハードルが高すぎるのではないでしょうか。
    – OOPer
    2018年9月15日 16:29
  • @OOPer いろいろ気を遣っていただいてありがとうございます。QtはやはりC++が強く、解説もC++側からの感覚でなされることが多いと昔から感じていました。C++に入ってみて、こういうことだったのか!という事もよくあります。Qtの経験とPythonの経験で被る部分が多いのですが、やはり細かいところは難しいなと。自分で下から簡単なクラスを組み立てて使えるようになるのがいいかなと考えるようになりました。(Qt Designerもやっと使う気になりました。)確かに難しいと思います。まだ環境のこともよくわかってないみたいなので、そういうところも含めて勉強していこうと思います。 2018年9月16日 4:35

0

この質問に回答するには、ログインする必要があります。

のタグが付いた他の質問を参照する。