QFont Object not recognized.
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
Code:
#ifndef STUFF_H
#define STUFF_H
#include <QWidget>
#include <QFont>
namespace Ui {
class Stuff;
}
{
Q_OBJECT
public:
explicit Stuff
(QWidget *parent
= 0);
~Stuff();
private slots:
void adjustFont();
private:
Ui::Stuff *ui;
QFont *font;
// font declared here! };
#endif
Code:
#include "stuff.h"
#include "ui_stuff.h"
#include <QButtonGroup>
#include <QFont>
ui(new Ui::Stuff)
{
ui->setupUi(this);
font
= new QFont("Times",
12);
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));
}
}
Re: QFont Object not recognized.
Quote:
I followed the documentation...
Then read again here what is the type of expected "setFont()" parameter (its not QFont *) :)
Re: QFont Object not recognized.
Ok now i'm confused.
It says
Code:
void setFont
( const QFont & )
, which means the paramter needs a reference to the font. So i passed a pointer which stores the reference. :S
Re: QFont Object not recognized.
Re: QFont Object not recognized.
I'm really sorry to be such a dufus,
but could you illustrate this with an unrelated code example, please?
Re: QFont Object not recognized.
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.
Code:
setFont
(QFont("Times",
12));
Re: QFont Object not recognized.
Code:
myWidget.setFont(font);
Re: QFont Object not recognized.
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"
Re: QFont Object not recognized.
Quote:
also i still get the error "invalid use of void expression"
Because code like this
Code:
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:
Code:
font->setBold(true);
font->...
ui->lineEdit->setFont( *font ); //!< if you dont know why its here ...
... read this
Re: QFont Object not recognized.
OMG, Stampede - that makes perfect sense now. I wasn't seeing the whole picture. Thanks alot to everyone!
Added after 23 minutes:
Quote:
Originally Posted by
FelixB
Code:
myWidget.setFont(font);
Quote:
Originally Posted by
FelixB
Pointer != Reference
could you also provide a link that explains this. I know its basic but i'm still a bit confused. Thanks alot!