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

Qt-interest Archive, April 2007
Modifying spreadsheets

Pages: Prev | 1 | 2 | Next

Message 1 in thread

I have set up a basic spreadsheet, and I would like to know if anyone out 
there could let me know how I can alter the colum headings ( instead of 
sayin A,B,C etc.) and limit the number of columns. Any help appreciated asap

thanks

_________________________________________________________________
Match.com - Click Here To Find Singles In Your Area Today! 
http://msnuk.match.com/

--
 [ signature omitted ] 

Message 2 in thread

---------- Forwarded message ----------
From: Martin Møller Pedersen <traxplayer@xxxxxxxxx>
Date: 12-Apr-2007 21:38
Subject: Re: Modifying spreadsheets
To: "qt-interest@xxxxxxxxxxxxx"


On 12/04/07, Marco Gallone <polo75cake@xxxxxxxxxxx> wrote:
> I have set up a basic spreadsheet, and I would like to know if anyone out
> there could let me know how I can alter the colum headings ( instead of
> sayin A,B,C etc.) and limit the number of columns. Any help appreciated asap
>
> thanks

Please show us some of your QT code and maybe we can help you.

/Martin


-- 
 [ signature omitted ] 

Message 3 in thread




>I have set up a basic spreadsheet, and I would like to know if anyone out 
>there could let me know how I can alter the colum headings ( instead of 
>saying A,B,C etc.) and limit the number of columns. In the section where it 
>says 'setText->(QString(QChar('A' + i)))' is the part thatI am trying to 
>modify, however i am having no success. Any help appreciated asap.
>
>thanks
>
>_________________________________________________________________
>Match.com - Click Here To Find Singles In Your Area Today! 
>http://msnuk.match.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/
>

_________________________________________________________________
Match.com - Click Here To Find Singles In Your Area Today! 
http://msnuk.match.com/

--
 [ signature omitted ] 

Message 4 in thread

On 4/12/07, Marco Gallone <polo75cake@xxxxxxxxxxx> wrote:
>
> >I have set up a basic spreadsheet, and I would like to know if anyone out
> >there could let me know how I can alter the colum headings ( instead of
> >saying A,B,C etc.) and limit the number of columns. In the section where it
> >says 'setText->(QString(QChar('A' + i)))' is the part thatI am trying to
> >modify, however i am having no success.

What widget are you using? Posting the relevant code would be helpful.

-- 
 [ signature omitted ] 

Message 5 in thread




Ok. I apologise to everyone for appearing so foolish in leaving out the code 
required in my previous emails. I guess its one of those mistakes you make 
when you're learning :). I did this spreadsheet as an example from a book, 
but I am altering it and adding extra features to increase my knowledge.
Right,

I have created a spreadsheet application, with a spreadsheet widget (or a 
QTable. This is an area where I am slightly confused) as my central widget. 
I would like to know how to change the headers, so that instead of reading 
'A, B C..' etc across the column headers, it reads different things suc as 
'First name, Surname....'.  I have set the column count to 10, so that I 
have the desired number of headers.

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.

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

for (int i= 0; i < Column Count; ++i) {

QTableWidgetItem *item = new QTableWidgetItem;
item->setText(QString(QChar('A' + i)));
setHorizontalHeaderItem(i, item);

}

setCurrentCell(0,0);
}


>>
>>thanks
>>

_________________________________________________________________
MSN Hotmail is evolving - check out the new Windows Live Mail 
http://ideas.live.co.uk

--
 [ signature omitted ] 

Message 6 in thread

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


> I have created a spreadsheet application, with a spreadsheet widget (or a
> QTable. This is an area where I am slightly confused) as my central
widget.
> I would like to know how to change the headers, so that instead of reading
> 'A, B C..' etc across the column headers, it reads different things suc as
> 'First name, Surname....'.  I have set the column count to 10, so that I
> have the desired number of headers.

Your question is still not clear.  The column headers are 'A','B','C' etc.
because you set them to that.  If you want to set them to something else
then do that.  Or are you asking a different question?

Assuming Qt4, I would probably use
setHorizontalHeaderLabels(QStringList);
instead of setHorizontalHeaderItem().

--
 [ signature omitted ] 

Message 7 in thread


>From: "Marco Gallone" <polo75cake@xxxxxxxxxxx>
>To: qt-interest@xxxxxxxxxxxxx
>Subject: FW: Modifying spreadsheets
>Date: Fri, 13 Apr 2007 09:21:40 +0000
>
>
>
>
>
>Ok. I apologise to everyone for appearing so foolish in leaving out the 
>code required in my previous emails. I guess its one of those mistakes you 
>make when you're learning :). I did this spreadsheet as an example from a 
>book, but I am altering it and adding extra features to increase my 
>knowledge.
>Right,
>
>I have created a spreadsheet application, with a spreadsheet widget (or a 
>QTable. This is an area where I am slightly confused) as my central widget. 
>I would like to know how to change the headers, so that instead of reading 
>'A, B C..' etc across the column headers, it reads different things suc as 
>'First name, Surname....'.  I have set the column count to 10, so that I 
>have the desired number of headers.
>
>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.
>>
>
>>>
>>>thanks
>>>
>
>_________________________________________________________________
>MSN Hotmail is evolving - check out the new Windows Live Mail 
>http://ideas.live.co.uk
>
>--
>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/
>

_________________________________________________________________
MSN Hotmail is evolving - check out the new Windows Live Mail.  
http://ideas.live.co.uk/
#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;
        item->setText(QString(QChar('A' + i )));
        setHorizontalHeaderLabels(QStringList)('Name' 'Surname' 'Handicap') ;

    }

    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;
    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 = 10 };

    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 8 in thread

"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 9 in thread

When compiling the updated code I get the error:  no match for call to 
(QString) (QStringList&).

I apologise for the amount of messagges I am sending, but your help is very 
appreciated.


>From: "Duane Hebert" <spoo@xxxxxxxxx>
>To: qt-interest@xxxxxxxxxxxxx
>Subject: Re: Modifying spreadsheets
>Date: Fri, 13 Apr 2007 09:10:08 -0400
>
>
>"Marco Gallone" <polo75cake@xxxxxxxxxxx> wrote in message
>news:BAY115-F2452D6FBD7A846F92378FDD5D0@xxxxxxxxxx
>
>
> > I have created a spreadsheet application, with a spreadsheet widget (or 
>a
> > QTable. This is an area where I am slightly confused) as my central
>widget.
> > I would like to know how to change the headers, so that instead of 
>reading
> > 'A, B C..' etc across the column headers, it reads different things suc 
>as
> > 'First name, Surname....'.  I have set the column count to 10, so that I
> > have the desired number of headers.
>
>Your question is still not clear.  The column headers are 'A','B','C' etc.
>because you set them to that.  If you want to set them to something else
>then do that.  Or are you asking a different question?
>
>Assuming Qt4, I would probably use
>setHorizontalHeaderLabels(QStringList);
>instead of setHorizontalHeaderItem().
>
>--
>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/
>

_________________________________________________________________
Solve the Conspiracy and win fantastic prizes.  
http://www.theconspiracygame.co.uk/

--
 [ signature omitted ] 

Message 10 in thread

"Marco Gallone" <polo75cake@xxxxxxxxxxx> wrote in message
news:BAY115-F39958831DC5E8128C77DADD5D0@xxxxxxxxxx
> When compiling the updated code I get the error:  no match for call to
> (QString) (QStringList&).
>
> I apologise for the amount of messagges I am sending, but your help is
very
> appreciated.

Have you included QString and QStringList?

--
 [ signature omitted ] 

Message 11 in thread

No, but I just included them in the file and still got the same error.


>From: "Duane Hebert" <spoo@xxxxxxxxx>
>To: qt-interest@xxxxxxxxxxxxx
>Subject: Re: Re: Modifying spreadsheets
>Date: Fri, 13 Apr 2007 16:30:13 -0400
>
>
>"Marco Gallone" <polo75cake@xxxxxxxxxxx> wrote in message
>news:BAY115-F39958831DC5E8128C77DADD5D0@xxxxxxxxxx
> > When compiling the updated code I get the error:  no match for call to
> > (QString) (QStringList&).
> >
> > I apologise for the amount of messagges I am sending, but your help is
>very
> > appreciated.
>
>Have you included QString and QStringList?
>
>--
>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 12 in thread

"Marco Gallone" <polo75cake@xxxxxxxxxxx> wrote in message
news:BAY115-F29854E49E1F653BB3CD031DD5D0@xxxxxxxxxx
> No, but I just included them in the file and still got the same error.

No idea then.  You need to find the line causing the
error and see what's wrong.  Do you not have a
debugger that shows the line the error is on?

--
 [ signature omitted ] 

Message 13 in thread

Yes, the error is on line 49.


>From: "Duane Hebert" <spoo@xxxxxxxxx>
>To: qt-interest@xxxxxxxxxxxxx
>Subject: Re: Re: Re: Modifying spreadsheets
>Date: Fri, 13 Apr 2007 16:33:37 -0400
>
>
>"Marco Gallone" <polo75cake@xxxxxxxxxxx> wrote in message
>news:BAY115-F29854E49E1F653BB3CD031DD5D0@xxxxxxxxxx
> > No, but I just included them in the file and still got the same error.
>
>No idea then.  You need to find the line causing the
>error and see what's wrong.  Do you not have a
>debugger that shows the line the error is on?
>
>--
>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/
>

_________________________________________________________________
Solve the Conspiracy and win fantastic prizes.  
http://www.theconspiracygame.co.uk/

--
 [ signature omitted ] 

Message 14 in thread

"Marco Gallone" <polo75cake@xxxxxxxxxxx> wrote in message
news:BAY115-F32B97C0E6EBBE985241F76DD5D0@xxxxxxxxxx
> Yes, the error is on line 49.

Funny, it's usually on line 42.  At any rate, fix
line 49 and you should be all set.

--
 [ signature omitted ] 

Message 15 in thread

Could you explain what you mean by fix?

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


>From: "Duane Hebert" <spoo@xxxxxxxxx>
>To: qt-interest@xxxxxxxxxxxxx
>Subject: Re: Re: Re: Re: Modifying spreadsheets
>Date: Fri, 13 Apr 2007 16:35:47 -0400
>
>
>"Marco Gallone" <polo75cake@xxxxxxxxxxx> wrote in message
>news:BAY115-F32B97C0E6EBBE985241F76DD5D0@xxxxxxxxxx
> > Yes, the error is on line 49.
>
>Funny, it's usually on line 42.  At any rate, fix
>line 49 and you should be all set.
>
>--
>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 ] 

Pages: Prev | 1 | 2 | Next