PDA

View Full Version : QFont Object not recognized.



eLancaster
8th February 2011, 12:19
Hi everyone,
i want to make a program with a text field and two check boxes: bold and italics which adjust the font in the text field accordingly.
I made QFont a private member in Stuff.h and initialized it in Stuff.cpp - but there seems to be some problem there. I followed the documentation so i dont understand why theres a problem.
the code and errors are mentioned below

Stuff.h

#ifndef STUFF_H
#define STUFF_H

#include <QWidget>
#include <QFont>


namespace Ui {
class Stuff;
}

class Stuff : public QWidget
{
Q_OBJECT

public:
explicit Stuff(QWidget *parent = 0);
~Stuff();

private slots:
void adjustFont();

private:
Ui::Stuff *ui;
QFont *font;// font declared here!
};

#endif




#include "stuff.h"
#include "ui_stuff.h"
#include <QButtonGroup>
#include <QFont>

Stuff::Stuff(QWidget *parent) :
QWidget(parent),
ui(new Ui::Stuff)
{
ui->setupUi(this);
font = new QFont("Times",12);


QButtonGroup *group = new QButtonGroup(this);
group->addButton(ui->SBoldButton);
group->addButton(ui->SItalicButton);
group->setExclusive(false);

ui->SLineEdit->setText("this is a line");
ui->SLineEdit->setFont(font);// /home/ubuntu/QtProjects/Stuff-build-desktop/../Stuff/stuff.cpp:23: error: no matching function for call to ‘QLineEdit::setFont(QFont*&)’

connect(group,SIGNAL(buttonClicked(int)),this,SLOT (adjustFont()));
connect(ui->SQuitButton,SIGNAL(clicked()),qApp,SLOT(quit()));
}

Stuff::~Stuff()
{
delete ui;
}

void Stuff::adjustFont()
{
Qt::CheckState boldState;
Qt::CheckState italicState;

boldState=ui->SBoldButton->checkState();
italicState=ui->SBoldButton->checkState();

if(boldState==Qt::Checked && italicState!=Qt::Checked)
{
ui->SLineEdit->setFont(font->setBold(true));///home/ubuntu/QtProjects/Stuff-build-desktop/../Stuff/stuff.cpp:44: error: invalid use of void expression
ui->SLineEdit->setFont(font->setItalic(false));//same as above and applies for all similar lines of code below
}

else if(boldState!=Qt::Checked && italicState==Qt::Checked)
{
ui->SLineEdit->setFont(font->setBold(false));
ui->SLineEdit->setFont(font->setItalic(true));
}

else if(boldState==Qt::Checked && italicState==Qt::Checked)
{
ui->SLineEdit->setFont(font->setBold(true));
ui->SLineEdit->setFont(font->setItalic(true));
}

else
{
ui->SLineEdit->setFont(font->setBold(false));
ui->SLineEdit->setFont(font->setItalic(false));
}
}

stampede
8th February 2011, 12:29
I followed the documentation...
Then read again here (http://doc.qt.nokia.com/latest/qwidget.html#font-prop) what is the type of expected "setFont()" parameter (its not QFont *) :)

eLancaster
8th February 2011, 13:08
Ok now i'm confused.
It says
void setFont ( const QFont & ), which means the paramter needs a reference to the font. So i passed a pointer which stores the reference. :S

FelixB
8th February 2011, 13:10
Pointer != Reference

eLancaster
8th February 2011, 13:12
I'm really sorry to be such a dufus,
but could you illustrate this with an unrelated code example, please?

MarekR22
8th February 2011, 13:16
I would say that QFont shouldn't be created on heap. Use of this class is similar to QString (implicit data sharing).
Note also that QWidfet::setFont has specific way of working! What is returned from QWidfet::font is compilation of fonts set for this widget its parent widgets and QApplication.

Just do it like that.

setFont(QFont("Times",12));

FelixB
8th February 2011, 13:19
QFont font("Times",12);
myWidget.setFont(font);

eLancaster
8th February 2011, 13:36
I still dont understand why you have to do that. I've never read of anything like that, i'm very sorry to say :$
also i still get the error "invalid use of void expression"

stampede
8th February 2011, 13:55
also i still get the error "invalid use of void expression"
Because code like this

ui->SLineEdit->setFont(font->setBold(false));
makes no sense: font->setBold() is a method that returns 'void', and "setFont()" requires 'const QFont&'.
Modify your font and then assign it to QLineEdits:


font->setBold(true);
font->...
ui->lineEdit->setFont( *font ); //!< if you dont know why its here ...

... read this (http://www.brpreiss.com/books/opus4/html/page589.html)

eLancaster
8th February 2011, 14:37
OMG, Stampede - that makes perfect sense now. I wasn't seeing the whole picture. Thanks alot to everyone!

Added after 23 minutes:



QFont font("Times",12);
myWidget.setFont(font);


Pointer != Reference

could you also provide a link that explains this. I know its basic but i'm still a bit confused. Thanks alot!