Results 1 to 10 of 10

Thread: QFont Object not recognized.

  1. #1
    Join Date
    Dec 2010
    Location
    My bed
    Posts
    21
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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
    Qt Code:
    1. #ifndef STUFF_H
    2. #define STUFF_H
    3.  
    4. #include <QWidget>
    5. #include <QFont>
    6.  
    7.  
    8. namespace Ui {
    9. class Stuff;
    10. }
    11.  
    12. class Stuff : public QWidget
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit Stuff(QWidget *parent = 0);
    18. ~Stuff();
    19.  
    20. private slots:
    21. void adjustFont();
    22.  
    23. private:
    24. Ui::Stuff *ui;
    25. QFont *font;// font declared here!
    26. };
    27.  
    28. #endif
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "stuff.h"
    2. #include "ui_stuff.h"
    3. #include <QButtonGroup>
    4. #include <QFont>
    5.  
    6. Stuff::Stuff(QWidget *parent) :
    7. QWidget(parent),
    8. ui(new Ui::Stuff)
    9. {
    10. ui->setupUi(this);
    11. font = new QFont("Times",12);
    12.  
    13.  
    14. QButtonGroup *group = new QButtonGroup(this);
    15. group->addButton(ui->SBoldButton);
    16. group->addButton(ui->SItalicButton);
    17. group->setExclusive(false);
    18.  
    19. ui->SLineEdit->setText("this is a line");
    20. ui->SLineEdit->setFont(font);// /home/ubuntu/QtProjects/Stuff-build-desktop/../Stuff/stuff.cpp:23: error: no matching function for call to ‘QLineEdit::setFont(QFont*&)’
    21.  
    22. connect(group,SIGNAL(buttonClicked(int)),this,SLOT(adjustFont()));
    23. connect(ui->SQuitButton,SIGNAL(clicked()),qApp,SLOT(quit()));
    24. }
    25.  
    26. Stuff::~Stuff()
    27. {
    28. delete ui;
    29. }
    30.  
    31. void Stuff::adjustFont()
    32. {
    33. Qt::CheckState boldState;
    34. Qt::CheckState italicState;
    35.  
    36. boldState=ui->SBoldButton->checkState();
    37. italicState=ui->SBoldButton->checkState();
    38.  
    39. if(boldState==Qt::Checked && italicState!=Qt::Checked)
    40. {
    41. ui->SLineEdit->setFont(font->setBold(true));///home/ubuntu/QtProjects/Stuff-build-desktop/../Stuff/stuff.cpp:44: error: invalid use of void expression
    42. ui->SLineEdit->setFont(font->setItalic(false));//same as above and applies for all similar lines of code below
    43. }
    44.  
    45. else if(boldState!=Qt::Checked && italicState==Qt::Checked)
    46. {
    47. ui->SLineEdit->setFont(font->setBold(false));
    48. ui->SLineEdit->setFont(font->setItalic(true));
    49. }
    50.  
    51. else if(boldState==Qt::Checked && italicState==Qt::Checked)
    52. {
    53. ui->SLineEdit->setFont(font->setBold(true));
    54. ui->SLineEdit->setFont(font->setItalic(true));
    55. }
    56.  
    57. else
    58. {
    59. ui->SLineEdit->setFont(font->setBold(false));
    60. ui->SLineEdit->setFont(font->setItalic(false));
    61. }
    62. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QFont Object not recognized.

    I followed the documentation...
    Then read again here what is the type of expected "setFont()" parameter (its not QFont *)

  3. #3
    Join Date
    Dec 2010
    Location
    My bed
    Posts
    21
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFont Object not recognized.

    Ok now i'm confused.
    It says
    Qt Code:
    1. void setFont ( const QFont & )
    To copy to clipboard, switch view to plain text mode 
    , which means the paramter needs a reference to the font. So i passed a pointer which stores the reference. :S

  4. #4
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QFont Object not recognized.

    Pointer != Reference

  5. #5
    Join Date
    Dec 2010
    Location
    My bed
    Posts
    21
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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?

  6. #6
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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.
    Qt Code:
    1. setFont(QFont("Times",12));
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QFont Object not recognized.

    Qt Code:
    1. QFont font("Times",12);
    2. myWidget.setFont(font);
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to FelixB for this useful post:

    eLancaster (8th February 2011)

  9. #8
    Join Date
    Dec 2010
    Location
    My bed
    Posts
    21
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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"

  10. #9
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QFont Object not recognized.

    also i still get the error "invalid use of void expression"
    Because code like this
    Qt Code:
    1. ui->SLineEdit->setFont(font->setBold(false));
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. font->setBold(true);
    2. font->...
    3. ui->lineEdit->setFont( *font ); //!< if you dont know why its here ...
    To copy to clipboard, switch view to plain text mode 
    ... read this

  11. The following user says thank you to stampede for this useful post:

    eLancaster (8th February 2011)

  12. #10
    Join Date
    Dec 2010
    Location
    My bed
    Posts
    21
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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 View Post
    Qt Code:
    1. QFont font("Times",12);
    2. myWidget.setFont(font);
    To copy to clipboard, switch view to plain text mode 
    Quote Originally Posted by FelixB View Post
    Pointer != Reference
    could you also provide a link that explains this. I know its basic but i'm still a bit confused. Thanks alot!
    Last edited by eLancaster; 8th February 2011 at 14:37.

Similar Threads

  1. Swipe gesture not recognized
    By Luc4 in forum Qt Programming
    Replies: 2
    Last Post: 16th December 2014, 15:13
  2. Qmake command not recognized
    By Rakula in forum Installation and Deployment
    Replies: 2
    Last Post: 15th September 2014, 13:10
  3. Resize Policy not recognized
    By Ishmael in forum Newbie
    Replies: 1
    Last Post: 20th May 2010, 08:46
  4. mingw32-make not recognized
    By freekill in forum Installation and Deployment
    Replies: 4
    Last Post: 8th January 2010, 16:26
  5. slots and signals don't seem to be recognized
    By Jessehk in forum Qt Programming
    Replies: 2
    Last Post: 31st July 2007, 19:50

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.