I'm trying to set the text colour for selected text in a spreadsheet (tablewidget). I've tried several different methods but none of them compile. First I wrote this code for changing the font. This works fine.
both functions on spreadsheet.cpp
void Spreadsheet::selectCellFont()
{
QList<QTableWidgetItem *> items = selectedItems();
if (items.isEmpty())
return; // nothing happens if nothing is selected and the dialog doesn't appear
bool ok = false;
if (!ok) // if set to OK nothing is changed
return;
if(item)
item->setFont(fnt);
// somethingChanged();
}
void Spreadsheet::selectCellFont()
{
QList<QTableWidgetItem *> items = selectedItems();
if (items.isEmpty())
return; // nothing happens if nothing is selected and the dialog doesn't appear
bool ok = false;
QFont fnt = QFontDialog::getFont(&ok, font(), this);
if (!ok) // if set to OK nothing is changed
return;
foreach (QTableWidgetItem *item, items)
if(item)
item->setFont(fnt);
// somethingChanged();
}
To copy to clipboard, switch view to plain text mode
I then wrote similar code for text colour
void SpreadSheet::selectFontColour()
{
QList<QTableWidgetItem *> items = selectedItems();
if (items.isEmpty())
return; // nothing happens if nothing is selected and the dialog doesn't appear
bool ok = false;
if (!ok)
return;
if (item)
item->setTextColor(col);
}
void SpreadSheet::selectFontColour()
{
QList<QTableWidgetItem *> items = selectedItems();
if (items.isEmpty())
return; // nothing happens if nothing is selected and the dialog doesn't appear
bool ok = false;
QColor col = QColorDialog::getColor(&ok, items->textColor(), this);
if (!ok)
return;
foreach (QTableWidgetItem *item, items)
if (item)
item->setTextColor(col);
}
To copy to clipboard, switch view to plain text mode
with prototypes in Spreadsheet class on spreadsheet.hpp
{
Q_OBJECT
public://
void clear();
void printHtml
(const QString &html
);
public slots://
void selectCellFont();
void selectFontColour();
// etc
class Spreadsheet : public QTableWidget
{
Q_OBJECT
public://
Spreadsheet(const QString &fileName = QString(),
QWidget *parent = 0);
QString currentLocation() const;
QString currentFormula() const;
void clear();
QTableWidgetSelectionRange selectedRange() const;
bool load(const QString &fileName = QString());
bool save(const QString &fileName = QString());
void printHtml(const QString &html);
public slots://
void selectCellFont();
void selectFontColour();
// etc
To copy to clipboard, switch view to plain text mode
I get these errors
src/spreadsheet.cpp:312: error: ‘SpreadSheet’ has not been declared
src/spreadsheet.cpp: In function ‘void selectFontColour()’:
src/spreadsheet.cpp:314: error: ‘selectedItems’ was not declared in this scope
src/spreadsheet.cpp:319: error: base operand of ‘->’ has non-pointer type ‘QList<QTableWidgetItem*>’
src/spreadsheet.cpp:319: error: invalid use of ‘this’ in non-member function
The first one is the most puzzling since the font function works.... Can anyone help?
Bookmarks