(e-mail address removed)...
You didn't give a lot of information so this is a guess.
Is autoRecalculate() a member function of something? What about
autoRecalc, what is that? Is it a function? If so the line should be
this:
bool autoRecalculate() const { return autoRecalc(); }
Note the parentheses. That calls autoRecalc() and returns the result,
rather than returning a function pointer which can't be converted to a
bool.
Is autoRecalc a variable? If it is, then your error is from something
else, you need to give more context.
Is autoRecalc a macro? If it is, see what it is #defined to. That may
be causing a problem.
Jason
Jason,
Thanks for your quick reply. I am pasting the spreadsheet.h and
spreadsheet.cpp files below for your review. `autoRecalculate` is a
member function and `autoRecalc` is a variable. See what you can make
from the files below...I probably have a stupid typo that I'm
missing...
------------------------------------------------
Begin file: spreadsheet.h
------------------------------------------------
#ifndef SPREADSHEET_H
#define SPREADSHEET_H
#include <qstringlist.h>
#include <qtable.h>
class Cell;
class SpreadsheetCompare;
class Spreadsheet : public QTable
{
Q_OBJECT
public:
Spreadsheet(QWidget *parent = 0, const char *name = 0);
void clear();
QString currentLocation() const;
QString currentFormula() const;
bool autoRecalculate() const { return autoRecalc; }
bool readFile(const QString &fileName);
bool writeFile(const QString &fileName);
QTableSelection selection();
void sort(const SpreadsheetCompare &compare);
public slots:
void cut();
void copy();
void past();
void del();
void selectRow();
void selectColumn();
void selectAll();
void recalculate();
void setAutoRecalculate(bool on);
void findNext(const QString &str, bool caseSensitive);
void findPrev(const QString &str, bool caseSensitive);
signals:
void modified();
protected:
QWidget *createEditor(int row, int col, bool initFromCell) const;
void endEdit(int row, int col, bool Accepted, bool wasReplacing);
private:
enum { MagicNumber = 0x7F51C882, NumRows = 999, NumCols = 26 };
Cell *cell(int row, int col) const;
void setFormula(int row, int col, const QString &formula);
QString formula(int row, int col) const;
void somethingChanged();
bool autoRecalc();
}; // End of Spreadsheet class definition.
class SpreadsheetCompare
{
public:
bool operator()(const QStringList &row1,
const QStringList &row2) const;
enum { NumKeys = 3 };
int keys[NumKeys];
bool ascending[NumKeys];
}; // End of SpreadsheetCompare class definition.
#endif
------------------------------------------------
End of file: spreadsheet.h
------------------------------------------------
------------------------------------------------
Begin file: spreadsheet.cpp
------------------------------------------------
#include <qapplication.h>
#include <qclipboard.h>
#include <qdatastream.h>
#include <qfile.h>
#include <qlineedit.h>
#include <qmessagebox.h>
#include <qregexp.h>
#include <qvariant.h>
#include <algorithm>
#include <vector>
using namespace std;
#include "cell.h"
#include "spreadsheet.h"
Spreadsheet::Spreadsheet(QWidget *parent, const char *name)
: QTable(parent, name)
{
autoRecalc = true;
setSelectionMode(Single);
clear();
} // End of Spreadsheet class constructor.
void Spreadsheet::clear()
{
setNumRows(0);
setNumCols(0);
setNumRows(NumRows);
setNumCols(NumCols);
for (int i = 0; i < NumCols; i++)
horizontalHeader()->setLabel(i, QChar('A' + i));
setCurrentCell(0, 0);
} // End of Spreadsheet::clear() public function.
Cell *Spreadsheet::cell(int row, int col) const
{
return (cell *)item(row, col);
} //Eend of Spreadsheet::cell() private function.
QString Spreadsheet::formula(int row, int col) const
{
Cell *c = cell(row, col);
if (c)
return c->formula();
else
return "";
} // End of Spreadsheet::formula() private function.
void Spreadsheet::setFormula(int row, int col,
const QString &formula)
{
Cell *c = cell(row, col);
if (c) {
c->setFormula(formula);
updateCell(row, col);
} else {
setItem(row, col, new Cell(this, formula));
}
} // End of Spreadsheet::setFormula() private function.
QString Spreadsheet::currentLocation() const
{
return QChar('A' + currentColumn())
+ QString::number(currentRow() + 1);
} // End of Spreadsheet::currentLocation() function.
QString Spreadsheet::currentFormula() const
{
return formula(currentRow(), currentColumn());
} // End of Spreadsheet::currentFormula() function.
QWidget *Spreadsheet::createEditor(int row, int col,
bool initFromCell) const
{
QLineEdit *lineEdit = new QLineEdit(viewport());
lineEdit->setFrame(false);
if (initFromCell)
lineEdit->setText(formula(row, col));
return lineEdit;
} // End of Spreadsheet::createEditor() function.
void Spreadsheet::endEdit(int row, int col, bool accepted,
bool wasReplacing)
{
QLineEdit *lineEdit = (QLineEdit *)cellWidget(row, col);
if (!lineEdit)
return;
QString oldFormula = formula(row, col);
QString newFormula = lineEdit->text();
QTable::endEdit(row, col, false, wasReplacing);
if (accepted && newFormula != oldFormula) {
setFormula(row, col, newFormula);
somethingChanged();
}
} // End of Spreadsheet::endEdit() function.
void Spreadsheet::somethingChanged()
{
if (autoRecalc)
recalculate();
emit modified();
} // End of Spreadsheet::somethingChanged() private function.
bool Spreadsheet::writeFile(const QString &fileName)
{
QFile file(fileName);
if (!file.open(IO_WriteOnly)) {
QMessageBox::warning(this, tr("Spreadsheet"),
tr("Cannot write file %1:\n%2.")
.arg(file.name())
.arg(file.errorString()));
return false;
}
QDataStream out(&file);
out.setVersion(5);
out << (Q_UINT32)MagicNumber;
QApplication::setOverrideCursor(waitCursor);
for (int row = 0; row < NumRows; ++row) {
for (int col = 0; col < NumCols; ++col) {
QString str = formula(row, col);
if (!str.isEmpty())
out << (Q_UINT16)row << (Q_UINT16)col << str;
}
}
QApplication::restoreOverrideCursor();
return true;
} // End of Spreadsheet::writeFile() function.
bool Spreadsheet::readFile(const QString &fileName)
{
QFile file(fileName);
if (!file.open(IO_ReadOnly)) {
QMessageBox::warning(this, tr("Spreadsheet"),
tr("Cannot read file %1:\n%2.")
.arg(file.name())
.arg(file.errorString()));
return false;
}
QDataStream in(&file);
in.setVersion(5);
Q_UINT32 magic;
in >> magic;
if (magic != MagicNumber) {
QMessageBox::warning(this, tr("Spreadsheet"),
tr("The file is not a "
"Spreadsheet file."));
return false;
}
clear();
Q_UINT16 row;
Q_UINT16 col;
QString str;
QApplication::setOverrideCursor(waitCursor);
while (!in.atEnd()) {
in >> row >> col >> str;
setFormula(row, col, str);
}
QApplication::restoreOverrideCursor();
return true;
} // End of Spreadsheet::readFile() function.
void Spreadsheet::cut()
{
copy();
del();
} // End of Spreadsheet::cut() function.
void Spreadsheet::copy()
{
QTableSelection sel = selection();
QString str;
for (int i = 0; i < sel.numRows(); ++i) {
if (i > 0)
str += "\n";
for (int j = 0; j < sel.numCols(); ++j) {
if (j > 0)
str += "\t";
str += formula(sel.topRow() + i, sel.leftCol() + j);
}
}
QApplication::clipboard()->setText(str);
} // End of Spreadsheet::copy() function.
QTableSelection Spreadsheet::selection()
{
if (QTable::selection(0).isEmpty())
return QTableSelection(currentRow(), currentColumn(),
currentRow(), currentColumn());
return QTable::selection(0);
} // End of Spreadsheet::selection() private function.
void Spreadsheet:
aste()
{
QTableSelection sel = selection();
QString str = QApplication::clipboard()->text();
QStringList rows = QStringList::split("\n", str, true);
int numRows = rows.size();
int numCols = rows.first().contains("\t") + 1;
if (sel.numRows() * sel.numCols() != 1
&& (sel.numRows() != numRows
|| sel.numCols() != numCols)) {
QMessageBox::information(this, tr("Spreadsheet"),
tr("The information cannot be pasted because the "
"copy and paste areas aren't the same size."));
return;
}
for (int i = 0; i < numRows; ++i) {
QStringList cols = QStringList::split("\t", rows
, true);
for (int j = 0; j < numCols; ++j) {
int row = sel.topRow() + i;
int col = sel.leftCol() + j;
if (row < NumRows && col < NumCols)
setFormula(row, col, cols[j]);
}
}
somethingChanged();
} // End of Spreadsheet:aste() function.
void Spreadsheet::del()
{
QTableSelection sel = selection();
for (int i = 0; i < sel.numRows(); ++i) {
for (int j = 0; j < sel.numCols(); ++j)
delete cell(sel.topRow() + i, sel.leftCol() + j);
}
clearSelection();
} // End of Spreadsheet::del() function.
void Spreadsheet::selectRow()
{
clearSelection();
QTable::selectRow(currentRow());
} // End of Spreadsheet::selectRow() function.
void Spreadsheet::selectColumn()
{
clearSelection();
QTable::selectColumn(currentColumn());
} // End of Spreadsheet::selectColumn() function.
void Spreadsheet::selectAll()
{
clearSelection();
selectCells(0, 0, NumRows - 1, NumCols - 1);
} // End of Spreadsheet::selectAll() function.
void Spreadsheet::findNext(const QString &str, bool caseSensitive)
{
int row = currentRow();
int col = currentColumn(); + 1;
while (row < NumRows) {
while (col < NumCols) {
if (text(row, col).contains(str, caseSensitive)) {
clearSelection();
setCurrentCell(row, col);
setActiveWindow();
return;
}
++col;
}
col = 0;
++row;
}
gApp->beep();
} // End of Spreadsheet::findNext() slot.
void Spreadsheet::findPrev(const QString &str, bool caseSensitive)
{
int row = currentRow();
int col = currentColumn() - 1;
while (row >=0) {
while (col >= 0) {
if (text(row, col).contains(str, caseSensitive)) {
clearSelection();
setCurrentCell(row, col);
setActiveWindow();
return;
}
--col;
}
col = NumCols -1;
--row;
}
gApp->beep();
} // End of Spreadsheet::findPrev() slot.
void Spreadsheet::recalculate()
{
int row;
for (row = 0; row < NumRows; ++row) {
for (int col = 0; col < NumCols; ++col) {
if (cell(row, col))
cell(row, col)->setDirty();
}
}
for (row = 0; row < NumRows; ++row) {
for (int col = 0; col < NumCols; ++col) {
if (cell(row, col))
updateCell(row, col);
}
}
} // End of Spreadsheet::recalculate() slot.
void Spreadsheet::setAutoRecalculate(bool on)
{
autoRecalc = on;
if (autoRecalc)
recalculate();
} // End of Spreadsheet::setAutoRecalculate() slot.
void Spreadsheet::sort(const SpreadsheetCompare &compare)
{
vector<QStringList> rows;
QTableSelection sel = selection();
int i;
for (i = 0; i < sel.numRows(); ++i) {
QStringList row;
for (int j = 0; j < sel.numCols(); ++j)
row.push_back(formula(sel.topRow() + i,
sel.leftCol() + j));
rows.push_back(row);
}
stable_sort(rows.begin(), rows.end(), compare);
for (i = 0; i < sel.numRows(); ++i) {
for (int j = 0; j < sel.numCols(); ++j)
setFormula(sel.topRow() + i, sel.leftCol() + j,
rows[j]);
}
clearSelection();
somethingChanged();
} // End of Spreadsheet::sort() slot.
bool SpreadsheetCompare:perator()(const QStringList &row1,
const QStringList &row2) const
{
for (int i = 0; i < NumKeys; ++i) {
int column = keys;
if (column != -1) {
if (row1[column] != row2[column]) {
if (ascending)
return row1[column] < row2[column];
else
return row1[column] > row2[column];
}
}
}
return false;
} // End of SpreadsheetCompare:perator() override function.
------------------------------------------------
End of file: spreadsheet.cpp
------------------------------------------------
Thanks again for any hints that you may be able to give me.
Cheers,
Sean Carrick
PekinSOFT Systems
PekinSOFT at gmail dot com