Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 4

Qt-interest Archive, April 2007
RE: Modifying spreadsheet


Message 1 in thread

I'm embarassed to ask this, but as a newbie to this, could you please tell 
me how to create a QStringList? What you said appears to be correct.

Thanks


    From: "Duane Hebert" <spoo@xxxxxxxxx>
To: qt-interest@xxxxxxxxxxxxx
Subject: Re: Modifying spreadsheets
Date: Fri, 13 Apr 2007 13:57:29 -0400


"Marco Gallone" <polo75cake@xxxxxxxxxxx> wrote in message
news:BAY115-F352A8923C623AEA18130F4DD5D0@xxxxxxxxxx

> >I am not sure if this IS the correct code, but maybe someone out there 
> >could tell me if it isn't, and give me a little nudge in the right 
> >direction. > I have included the files where the code is located.

Looks like you changed your clear() function to :

for (int i = 0; i < ColumnCount; ++i) {
        QTableWidgetItem *item = new QTableWidgetItem;
        item->setText(QString(QChar('A' + i )));
        setHorizontalHeaderLabels(QStringList)('Name' 'Surname' 'Handicap')
;

    }

setHorizontalLabels() shouldn't be in a loop like this and
the item->setText() call is a waste of time.  What is your
actual problem?  If you just want to set the header lables,
create a QStringList with the labels and call setHorizontalLabel()
somewhere.  If this isn't your problem can you explain what it is?

--
 [ signature omitted ] 

Message 2 in thread

"Marco Gallone" <polo75cake@xxxxxxxxxxx> wrote in message
news:BAY115-F29241ED29549B864F2E0CDD5D0@xxxxxxxxxx
> I'm embarassed to ask this, but as a newbie to this, could you please tell
> me how to create a QStringList? What you said appears to be correct.

Qt has excellent help.  From the docs for QStringList you can see
that it has a << operator and there is also an example:

QStringList fonts;
fonts << "Arial" << "Helvetica" << "Times" << "Courier";

or since it's a const & you can just do

setHorizontalHeaderLabels(QStringList() << "Name" << "Surname" <<
"Handicap");


HTH

--
 [ signature omitted ] 

Message 3 in thread

When I try this, I get some errors. I have written these below to see if you 
could give us a nudge in the right direction.

-No Match for call to '(QStringList)(QStringList&)'
-'setHorizontalLabels' undeclared (first use this function)

thanks


>From: "Duane Hebert" <spoo@xxxxxxxxx>
>To: qt-interest@xxxxxxxxxxxxx
>Subject: Re: Modifying spreadsheet
>Date: Fri, 13 Apr 2007 15:50:14 -0400
>
>
>"Marco Gallone" <polo75cake@xxxxxxxxxxx> wrote in message
>news:BAY115-F29241ED29549B864F2E0CDD5D0@xxxxxxxxxx
> > I'm embarassed to ask this, but as a newbie to this, could you please 
>tell
> > me how to create a QStringList? What you said appears to be correct.
>
>Qt has excellent help.  From the docs for QStringList you can see
>that it has a << operator and there is also an example:
>
>QStringList fonts;
>fonts << "Arial" << "Helvetica" << "Times" << "Courier";
>
>or since it's a const & you can just do
>
>setHorizontalHeaderLabels(QStringList() << "Name" << "Surname" <<
>"Handicap");
>
>
>HTH
>
>--
>To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
>"unsubscribe" in the subject or the body.
>List archive and information: http://lists.trolltech.com/qt-interest/
>

_________________________________________________________________
Get Hotmail, News, Sport and Entertainment from MSN on your mobile.  
http://www.msn.txt4content.com/

--
 [ signature omitted ] 

Message 4 in thread

"Marco Gallone" <polo75cake@xxxxxxxxxxx> wrote in message
news:BAY115-F359D08384769404AD735FDD5D0@xxxxxxxxxx
> When I try this, I get some errors. I have written these below to see if
you
> could give us a nudge in the right direction.
>
> -No Match for call to '(QStringList)(QStringList&)'
> -'setHorizontalLabels' undeclared (first use this function)

You have a typo.  It should be
setHorizontalHeaderLabels(QStringList() << "Name" << "Surname" <<
> >"Handicap");

as shown below.

Which version of Qt are you using?




>
> >From: "Duane Hebert" <spoo@xxxxxxxxx>
> >To: qt-interest@xxxxxxxxxxxxx
> >Subject: Re: Modifying spreadsheet
> >Date: Fri, 13 Apr 2007 15:50:14 -0400
> >
> >
> >"Marco Gallone" <polo75cake@xxxxxxxxxxx> wrote in message
> >news:BAY115-F29241ED29549B864F2E0CDD5D0@xxxxxxxxxx
> > > I'm embarassed to ask this, but as a newbie to this, could you please
> >tell
> > > me how to create a QStringList? What you said appears to be correct.
> >
> >Qt has excellent help.  From the docs for QStringList you can see
> >that it has a << operator and there is also an example:
> >
> >QStringList fonts;
> >fonts << "Arial" << "Helvetica" << "Times" << "Courier";
> >
> >or since it's a const & you can just do
> >
> >setHorizontalHeaderLabels(QStringList() << "Name" << "Surname" <<
> >"Handicap");
> >
> >
> >HTH
> >
> >--
> >To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> >"unsubscribe" in the subject or the body.
> >List archive and information: http://lists.trolltech.com/qt-interest/
> >
>
> _________________________________________________________________
> Get Hotmail, News, Sport and Entertainment from MSN on your mobile.
> http://www.msn.txt4content.com/
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
"unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/

--
 [ signature omitted ] 

Message 5 in thread

"Duane Hebert" <spoo@xxxxxxxxx> wrote in message
news:evoo9f$atj$1@xxxxxxxxxxxxxxxxxxxxx
>
> "Marco Gallone" <polo75cake@xxxxxxxxxxx> wrote in message
> news:BAY115-F359D08384769404AD735FDD5D0@xxxxxxxxxx
> > When I try this, I get some errors. I have written these below to see if
> you
> > could give us a nudge in the right direction.
> >
> > -No Match for call to '(QStringList)(QStringList&)'
> > -'setHorizontalLabels' undeclared (first use this function)
>
> You have a typo.  It should be
> setHorizontalHeaderLabels(QStringList() << "Name" << "Surname" <<

>  >"Handicap");

These two >> are from the mail reader of course <g>

--
 [ signature omitted ] 

Message 6 in thread

"Marco Gallone" <polo75cake@xxxxxxxxxxx> wrote in message
news:BAY115-F359D08384769404AD735FDD5D0@xxxxxxxxxx
> When I try this, I get some errors. I have written these below to see if
you
> could give us a nudge in the right direction.
>
> -No Match for call to '(QStringList)(QStringList&)'
> -'setHorizontalLabels' undeclared (first use this function)

The code you posted had:
 setHorizontalHeaderLabels(QStringList)('Name' 'Surname' 'Handicap')
which would account for the first error.

--
 [ signature omitted ] 

Message 7 in thread

I am using the version of Qt4 that was supplied wit the book 'C++ GUI 
Programming with QT4'


>From: "Duane Hebert" <spoo@xxxxxxxxx>
>To: qt-interest@xxxxxxxxxxxxx
>Subject: Re: Modifying spreadsheet
>Date: Fri, 13 Apr 2007 16:13:38 -0400
>
>
>"Marco Gallone" <polo75cake@xxxxxxxxxxx> wrote in message
>news:BAY115-F359D08384769404AD735FDD5D0@xxxxxxxxxx
> > When I try this, I get some errors. I have written these below to see if
>you
> > could give us a nudge in the right direction.
> >
> > -No Match for call to '(QStringList)(QStringList&)'
> > -'setHorizontalLabels' undeclared (first use this function)
>
>You have a typo.  It should be
>setHorizontalHeaderLabels(QStringList() << "Name" << "Surname" <<
> > >"Handicap");
>
>as shown below.
>
>Which version of Qt are you using?
>
>
>
>
> >
> > >From: "Duane Hebert" <spoo@xxxxxxxxx>
> > >To: qt-interest@xxxxxxxxxxxxx
> > >Subject: Re: Modifying spreadsheet
> > >Date: Fri, 13 Apr 2007 15:50:14 -0400
> > >
> > >
> > >"Marco Gallone" <polo75cake@xxxxxxxxxxx> wrote in message
> > >news:BAY115-F29241ED29549B864F2E0CDD5D0@xxxxxxxxxx
> > > > I'm embarassed to ask this, but as a newbie to this, could you 
>please
> > >tell
> > > > me how to create a QStringList? What you said appears to be correct.
> > >
> > >Qt has excellent help.  From the docs for QStringList you can see
> > >that it has a << operator and there is also an example:
> > >
> > >QStringList fonts;
> > >fonts << "Arial" << "Helvetica" << "Times" << "Courier";
> > >
> > >or since it's a const & you can just do
> > >
> > >setHorizontalHeaderLabels(QStringList() << "Name" << "Surname" <<
> > >"Handicap");
> > >
> > >
> > >HTH
> > >
> > >--
> > >To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> > >"unsubscribe" in the subject or the body.
> > >List archive and information: http://lists.trolltech.com/qt-interest/
> > >
> >
> > _________________________________________________________________
> > Get Hotmail, News, Sport and Entertainment from MSN on your mobile.
> > http://www.msn.txt4content.com/
> >
> > --
> > To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
>"unsubscribe" in the subject or the body.
> > List archive and information: http://lists.trolltech.com/qt-interest/
>
>--
>To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
>"unsubscribe" in the subject or the body.
>List archive and information: http://lists.trolltech.com/qt-interest/
>

_________________________________________________________________
Txt a lot? Get Messenger FREE on your mobile.  
https://livemessenger.mobile.uk.msn.com/

--
 [ signature omitted ] 

Message 8 in thread

"Marco Gallone" <polo75cake@xxxxxxxxxxx> wrote in message
news:BAY115-F118A02DDE989832DEDF9FFDD5D0@xxxxxxxxxx
> I am using the version of Qt4 that was supplied wit the book 'C++ GUI
> Programming with QT4'

That should be fine.  Check Qt assistant help for QTableWidget
and you should see the setHorizontalHeaderLabel() function.  It should
work once you fix the typos though.


--
 [ signature omitted ] 

Message 9 in thread

I got those errors after making the changes that you suggested. I have 
included the modified file.


>From: "Duane Hebert" <spoo@xxxxxxxxx>
>To: qt-interest@xxxxxxxxxxxxx
>Subject: Re: Modifying spreadsheet
>Date: Fri, 13 Apr 2007 16:16:09 -0400
>
>
>"Marco Gallone" <polo75cake@xxxxxxxxxxx> wrote in message
>news:BAY115-F359D08384769404AD735FDD5D0@xxxxxxxxxx
> > When I try this, I get some errors. I have written these below to see if
>you
> > could give us a nudge in the right direction.
> >
> > -No Match for call to '(QStringList)(QStringList&)'
> > -'setHorizontalLabels' undeclared (first use this function)
>
>The code you posted had:
>  setHorizontalHeaderLabels(QStringList)('Name' 'Surname' 'Handicap')
>which would account for the first error.
>
>--
>To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
>"unsubscribe" in the subject or the body.
>List archive and information: http://lists.trolltech.com/qt-interest/
>

_________________________________________________________________
Txt a lot? Get Messenger FREE on your mobile. 
https://livemessenger.mobile.uk.msn.com/
#include <QtGui>
#include <QtGui>

#include "cell.h"
#include "spreadsheet.h"

Spreadsheet::Spreadsheet(QWidget *parent)
    : QTableWidget(parent)
{
    autoRecalc = true;

    setItemPrototype(new Cell);
    setSelectionMode(ContiguousSelection);

    connect(this, SIGNAL(itemChanged(QTableWidgetItem *)),
            this, SLOT(somethingChanged()));

    clear();
}

QString Spreadsheet::currentLocation() const
{
    return QChar('A' + currentColumn())
           + QString::number(currentRow() + 1);
}

QString Spreadsheet::currentFormula() const
{
    return formula(currentRow(), currentColumn());
}

QTableWidgetSelectionRange Spreadsheet::selectedRange() const
{
    QList<QTableWidgetSelectionRange> ranges = selectedRanges();
    if (ranges.isEmpty())
        return QTableWidgetSelectionRange();
    return ranges.first();
}

void Spreadsheet::clear()
{
    setRowCount(0);
    setColumnCount(0);
    setRowCount(RowCount);
    setColumnCount(ColumnCount);

    for (int i = 0; i < ColumnCount; ++i) {
        QTableWidgetItem *item = new QTableWidgetItem;
        setHorizontalHeaderLabels(QStringList() << "Name" << "Surname" <<
> >"Handicap");

        setHorizontalLabels();
    }

    setCurrentCell(0, 0);
}

bool Spreadsheet::readFile(const QString &fileName)
{
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly)) {
        QMessageBox::warning(this, tr("Spreadsheet"),
                             tr("Cannot read file %1:\n%2.")
                             .arg(file.fileName())
                             .arg(file.errorString()));
        return false;
    }

    QDataStream in(&file);
    in.setVersion(QDataStream::Qt_4_1);

    quint32 magic;
    in >> magic;
    if (magic != MagicNumber) {
        QMessageBox::warning(this, tr("Spreadsheet"),
                             tr("The file is not a Spreadsheet file."));
        return false;
    }

    clear();

    quint16 row;
    quint16 column;
    QString str;

    QApplication::setOverrideCursor(Qt::WaitCursor);
    while (!in.atEnd()) {
        in >> row >> column >> str;
        setFormula(row, column, str);
    }
    QApplication::restoreOverrideCursor();
    return true;
}

bool Spreadsheet::writeFile(const QString &fileName)
{
    QFile file(fileName);
    if (!file.open(QIODevice::WriteOnly)) {
        QMessageBox::warning(this, tr("Spreadsheet"),
                             tr("Cannot write file %1:\n%2.")
                             .arg(file.fileName())
                             .arg(file.errorString()));
        return false;
    }

    QDataStream out(&file);
    out.setVersion(QDataStream::Qt_4_1);

    out << quint32(MagicNumber);

    QApplication::setOverrideCursor(Qt::WaitCursor);
    for (int row = 0; row < RowCount; ++row) {
        for (int column = 0; column < ColumnCount; ++column) {
            QString str = formula(row, column);
            if (!str.isEmpty())
                out << quint16(row) << quint16(column) << str;
        }
    }
    QApplication::restoreOverrideCursor();
    return true;
}

void Spreadsheet::sort(const SpreadsheetCompare &compare)
{
    QList<QStringList> rows;
    QTableWidgetSelectionRange range = selectedRange();
    int i;

    for (i = 0; i < range.rowCount(); ++i) {
        QStringList row;
        for (int j = 0; j < range.columnCount(); ++j)
            row.append(formula(range.topRow() + i,
                               range.leftColumn() + j));
        rows.append(row);
    }

    qStableSort(rows.begin(), rows.end(), compare);

    for (i = 0; i < range.rowCount(); ++i) {
        for (int j = 0; j < range.columnCount(); ++j)
            setFormula(range.topRow() + i, range.leftColumn() + j,
                       rows[i][j]);
    }

    clearSelection();
    somethingChanged();
}

void Spreadsheet::cut()
{
    copy();
    del();
}

void Spreadsheet::copy()
{
    QTableWidgetSelectionRange range = selectedRange();
    QString str;

    for (int i = 0; i < range.rowCount(); ++i) {
        if (i > 0)
            str += "\n";
        for (int j = 0; j < range.columnCount(); ++j) {
            if (j > 0)
                str += "\t";
            str += formula(range.topRow() + i, range.leftColumn() + j);
        }
    }
    QApplication::clipboard()->setText(str);
}

void Spreadsheet::paste()
{
    QTableWidgetSelectionRange range = selectedRange();
    QString str = QApplication::clipboard()->text();
    QStringList rows = str.split('\n');
    int numRows = rows.count();
    int numColumns = rows.first().count('\t') + 1;

    if (range.rowCount() * range.columnCount() != 1
            && (range.rowCount() != numRows
                || range.columnCount() != numColumns)) {
        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 columns = rows[i].split('\t');
        for (int j = 0; j < numColumns; ++j) {
            int row = range.topRow() + i;
            int column = range.leftColumn() + j;
            if (row < RowCount && column < ColumnCount)
                setFormula(row, column, columns[j]);
        }
    }
    somethingChanged();
}

void Spreadsheet::del()
{
    foreach (QTableWidgetItem *item, selectedItems())
        delete item;
}

void Spreadsheet::selectCurrentRow()
{
    selectRow(currentRow());
}

void Spreadsheet::selectCurrentColumn()
{
    selectColumn(currentColumn());
}

void Spreadsheet::recalculate()
{
    for (int row = 0; row < RowCount; ++row) {
        for (int column = 0; column < ColumnCount; ++column) {
            if (cell(row, column))
                cell(row, column)->setDirty();
        }
    }
    viewport()->update();
}

void Spreadsheet::setAutoRecalculate(bool recalc)
{
    autoRecalc = recalc;
    if (autoRecalc)
        recalculate();
}

void Spreadsheet::findNext(const QString &str, Qt::CaseSensitivity cs)
{
    int row = currentRow();
    int column = currentColumn() + 1;

    while (row < RowCount) {
        while (column < ColumnCount) {
            if (text(row, column).contains(str, cs)) {
                clearSelection();
                setCurrentCell(row, column);
                activateWindow();
                return;
            }
            ++column;
        }
        column = 0;
        ++row;
    }
    QApplication::beep();
}

void Spreadsheet::findPrevious(const QString &str,
                               Qt::CaseSensitivity cs)
{
    int row = currentRow();
    int column = currentColumn() - 1;

    while (row >= 0) {
        while (column >= 0) {
            if (text(row, column).contains(str, cs)) {
                clearSelection();
                setCurrentCell(row, column);
                activateWindow();
                return;
            }
            --column;
        }
        column = ColumnCount - 1;
        --row;
    }
    QApplication::beep();
}

void Spreadsheet::somethingChanged()
{
    if (autoRecalc)
        recalculate();
    emit modified();
}

Cell *Spreadsheet::cell(int row, int column) const
{
    return static_cast<Cell *>(item(row, column));
}

void Spreadsheet::setFormula(int row, int column,
                             const QString &formula)
{
    Cell *c = cell(row, column);
    if (!c) {
        c = new Cell;
        setItem(row, column, c);
    }
    c->setFormula(formula);
}

QString Spreadsheet::formula(int row, int column) const
{
    Cell *c = cell(row, column);
    if (c) {
        return c->formula();
    } else {
        return "";
    }
}

QString Spreadsheet::text(int row, int column) const
{
    Cell *c = cell(row, column);
    if (c) {
        return c->text();
    } else {
        return "";
    }
}

bool SpreadsheetCompare::operator()(const QStringList &row1,
                                    const QStringList &row2) const
{
    for (int i = 0; i < KeyCount; ++i) {
        int column = keys[i];
        if (column != -1) {
            if (row1[column] != row2[column]) {
                if (ascending[i]) {
                    return row1[column] < row2[column];
                } else {
                    return row1[column] > row2[column];
                }
            }
        }
    }
    return false;
}

#ifndef SPREADSHEET_H
#ifndef SPREADSHEET_H
#define SPREADSHEET_H

#include <QTableWidget>

class Cell;
class SpreadsheetCompare;

class Spreadsheet : public QTableWidget
{
    Q_OBJECT

public:
    Spreadsheet(QWidget *parent = 0);

    bool autoRecalculate() const { return autoRecalc; }
    QString currentLocation() const;
    QStringList(setHorizontalHeaderLabels);
    QString currentFormula() const;
    QTableWidgetSelectionRange selectedRange() const;

    void clear();
    bool readFile(const QString &fileName);
    bool writeFile(const QString &fileName);
    void sort(const SpreadsheetCompare &compare);

public slots:
    void cut();
    void copy();
    void paste();
    void del();
    void selectCurrentRow();
    void selectCurrentColumn();
    void recalculate();
    void setAutoRecalculate(bool recalc);
    void findNext(const QString &str, Qt::CaseSensitivity cs);
    void findPrevious(const QString &str, Qt::CaseSensitivity cs);

signals:
    void modified();

private slots:
    void somethingChanged();

private:
    enum { MagicNumber = 0x7F51C883, RowCount = 2000, ColumnCount = 3 };

    Cell *cell(int row, int column) const;
    QString text(int row, int column) const;
    QString formula(int row, int column) const;
    void setFormula(int row, int column, const QString &formula);

    bool autoRecalc;
};

class SpreadsheetCompare
{
public:
    bool operator()(const QStringList &row1,
                    const QStringList &row2) const;

    enum { KeyCount = 3 };
    int keys[KeyCount];
    bool ascending[KeyCount];
};

#endif


Message 10 in thread

"Marco Gallone" <polo75cake@xxxxxxxxxxx> wrote in message
news:BAY115-F3214B0FAD605B98AA7E91FDD5D0@xxxxxxxxxx
> I got those errors after making the changes that you suggested. I have
> included the modified file.

Your clear function looks like:

void Spreadsheet::clear()
{
    setRowCount(0);
    setColumnCount(0);
    setRowCount(RowCount);
    setColumnCount(ColumnCount);

    for (int i = 0; i < ColumnCount; ++i) {
        QTableWidgetItem *item = new QTableWidgetItem;
        setHorizontalHeaderLabels(QStringList() << "Name" << "Surname" <<
> >"Handicap");

        setHorizontalLabels();
    }

    setCurrentCell(0, 0);
}

Change it to:

void Spreadsheet::clear()
{
    setRowCount(0);
    setColumnCount(0);
    setRowCount(RowCount);
    setColumnCount(ColumnCount);

    setHorizontalHeaderLabels(QStringList() << "Name" << "Surname" <<
"Handicap");

    setCurrentCell(0, 0);
}

and it should compile.

--
 [ signature omitted ] 

Message 11 in thread

Could you please send me the file with the typos corrected.
Thanks


>From: "Duane Hebert" <spoo@xxxxxxxxx>
>To: qt-interest@xxxxxxxxxxxxx
>Subject: Re: Re: Modifying spreadsheet
>Date: Fri, 13 Apr 2007 16:20:48 -0400
>
>
>"Marco Gallone" <polo75cake@xxxxxxxxxxx> wrote in message
>news:BAY115-F118A02DDE989832DEDF9FFDD5D0@xxxxxxxxxx
> > I am using the version of Qt4 that was supplied wit the book 'C++ GUI
> > Programming with QT4'
>
>That should be fine.  Check Qt assistant help for QTableWidget
>and you should see the setHorizontalHeaderLabel() function.  It should
>work once you fix the typos though.
>
>
>--
>To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
>"unsubscribe" in the subject or the body.
>List archive and information: http://lists.trolltech.com/qt-interest/
>

_________________________________________________________________
Get Hotmail, News, Sport and Entertainment from MSN on your mobile.  
http://www.msn.txt4content.com/

--
 [ signature omitted ] 

Message 12 in thread

"Marco Gallone" <polo75cake@xxxxxxxxxxx> wrote in message
news:BAY115-F26A9CCC6BE75A115005C06DD5D0@xxxxxxxxxx
> Could you please send me the file with the typos corrected.
> Thanks

No.

--
 [ signature omitted ]