Results 1 to 2 of 2

Thread: Proper way to pass QColor or QFont to a Qdialog subclass?

  1. #1
    Join Date
    Apr 2017
    Posts
    21
    Qt products
    Qt5
    Platforms
    Windows

    Default Proper way to pass QColor or QFont to a Qdialog subclass?

    Hi,

    I am having a problem with a QDialog subclass that I am using to pass two QT Objects into: QColor and QFont. Basically my subclass looks like this:

    HEADER:

    class MySettingsDialog : public QDialog
    {
    Q_OBJECT

    public:
    explicit MySettingsDialog(QColor *acolor, QFont *afont, QWidget *parent=0);

    CLASS FILE:
    MySettingsDialog::MySettingsDialog(QColor *acolor, QFont *afont, QWidget *parent) :
    QDialog(parent, Qt::WindowCloseButtonHint), m_color(*acolor), m_font(*afont),
    ui(new Ui::MySettingsDialog)
    {

    ui->setupUi(this);
    }


    This way works but I have to in my MainWindow.cpp file declare for example:
    QFont * systemFont;

    systemFont = New QFont("myfont whatever");

    And everywhere I want to apply that font I have to unmask like this ui->mylabel->setfont(*systemFont).

    Before I wasn't using 'new' to instantiate my systemFont variable and systemFont wasn't declared as a pointer in the header file.
    Such that if Mydialog was declared like this the program crashes:

    explicit MySettingsDialog(QColor &acolor, QFont &afont, QWidget *parent=0);

    CLASS FILE:
    MySettingsDialog::MySettingsDialog(QColor &acolor, QFont &afont, QWidget *parent) :
    QDialog(parent, Qt::WindowCloseButtonHint), m_color(acolor), m_font(afont),
    ui(new Ui::MySettingsDialog)
    {

    ui->setupUi(this);
    }


    What am I doing wrong?

    Thanks

  2. #2
    Join Date
    Jul 2008
    Posts
    447
    Thanks
    9
    Thanked 58 Times in 56 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Proper way to pass QColor or QFont to a Qdialog subclass?

    Hi, you can create a pointer to a "normal" variable using the & operator.

    Please show the code where you create your systemFont when you don't use new. Possibly a local variable goes out of scope before MySettingsDialog uses it.

    Ginsengelf

Similar Threads

  1. pass ui to subclass
    By phil333 in forum Qt Programming
    Replies: 3
    Last Post: 20th March 2017, 03:34
  2. Replies: 5
    Last Post: 11th November 2016, 13:07
  3. Replies: 1
    Last Post: 12th September 2012, 19:26
  4. Replies: 5
    Last Post: 1st March 2010, 15:55

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.