how to use QBox correctly?



  • Hello there,
    I'm a Qt 5.6.2 user with visual studio 2015 on Windows 7 64bit.
    The code below is programmed for Qt 4.x with visual studio 2010 on 32 bit.
    When I try to compile this program, I get a lot of C2027 and C2227 errors.
    C2027: use of undefined type 'QVBoxLayout'
    C2027: use of undefined type 'QGroupBox'
    C2227: left of '->setTitle' must point to class/struct/union/generic type
    C2027: use of undefined type 'QGridLayout'
    C2227: left of '->addWidget' must point to class/struct/union/generic type
    C2027: use of undefined type 'QCheckBox'
    C2227: left of '->setTitle' must point to class/struct/union/generic type

    Header:

    #pragma once
    #include <qdialog.h>
    class QCheckBox;
    class QGridLayout;
    class QVBoxLayout;
    class QDialogButtonBox;
    class QGroupBox;

    class ChannelsDlg : public QDialog
    {
    Q_OBJECT

    public:
    ChannelsDlg(unsigned int*);
    virtual int exec();
    void getChannels(unsigned int*);

    private:
    unsigned int m_channels[4];
    QCheckBox* m_channel[128];

    private slots:
    void accept();
    void onCheckBoard(bool);
    };

    Source:

    #include "stdafx.h"
    #include "ChannelsDlg.h"

    // initialize the channel data
    ChannelsDlg::ChannelsDlg(unsigned int* channels)
    {
    int i;
    for (i = 0; i < 4; i++)
    {
    m_channels[i] = channels[i];
    }
    }

    // user checked on a "board" or "all channels"
    void ChannelsDlg::onCheckBoard(bool checked)
    {
    QWidget* w = focusWidget();
    int i, j, start, end, board = w ? w->property("board").toInt() : 0;

    start = board > 3 ? 0 : board;
    end = board > 3 ? 4 : board + 1;
    
    for (j = start; j < end; j++)
    {
        for (i = 0; i < 32; i++)
        {
            m_channel[(j * 32) + i]->setChecked(checked);
        }
    }
    

    }

    // save the check boxes to the channel bitmask
    void ChannelsDlg::accept()
    {
    int i, index;
    m_channels[0] = m_channels[1] = m_channels[2] = m_channels[3] = 0;

    for (i = 0; i < 128; i++)
    {
        index = i / 32;
        m_channels[index] = m_channel[i]->isChecked() ? m_channels[index] | 1 << (i % 32) : m_channels[index];
    }
    
    QDialog::accept();
    

    }

    void ChannelsDlg::getChannels(unsigned int* channels)
    {
    int i;
    for (i = 0; i < 4; i++)
    {
    channels[i] = m_channels[i];
    }
    }

    // setup the dialog manually
    int ChannelsDlg::exec()
    {
    QVBoxLayout* vl = new QVBoxLayout;
    QDialogButtonBox* bb;
    QCheckBox* cb;
    QGroupBox* board;
    QGridLayout* gl;
    int i, index, row, ch;

    for (i = 0; i < 4; i++)
    {
        board = new QGroupBox;
        board->setTitle(QString(tr("Board %1")).arg(i + 1));
        gl = new QGridLayout;
    
        for (row = 0; row < 4; row++)
        {
            for (ch = 0; ch < 8; ch++)
            {
                index = (i * 32) + (row * 8) + ch;
                m_channel[index] = new QCheckBox;
                m_channel[index]->setText(QString("Ch: %1").arg(index));
                m_channel[index]->setChecked((m_channels[i] & (1 << ((row * 8) + ch))) != 0);
                gl->addWidget(m_channel[index], row, ch);
            }
        }
    
        board->setLayout(gl);
    
        cb = new QCheckBox;
        cb->setText(QString(tr("Entire Board %1")).arg(i + 1));
        cb->setProperty("board", i);
        cb->setChecked(true);
        connect(cb, SIGNAL(toggled(bool)), this, SLOT(onCheckBoard(bool)));
        vl->addWidget(cb);
        vl->addWidget(board);
    }
    
    cb = new QCheckBox;
    cb->setText(QString(tr("All Channels")));
    cb->setProperty("board", 10);
    cb->setChecked(true);
    connect(cb, SIGNAL(toggled(bool)), this, SLOT(onCheckBoard(bool)));
    vl->addWidget(cb);
    
    // make sure user can use OK & Cancel
    bb = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    connect(bb, SIGNAL(accepted()), this, SLOT(accept()));
    connect(bb, SIGNAL(rejected()), this, SLOT(reject()));
    vl->addWidget(bb);
    
    setLayout(vl);
    resize(400, 400);
    setWindowTitle(tr("Channel Selection"));
    
    return QDialog::exec();
    

    }

    Source:
    #include "stdafx.h"
    #include "ChannelsDlg.h"

    // initialize the channel data
    ChannelsDlg::ChannelsDlg(unsigned int* channels)
    {
    int i;
    for (i = 0; i < 4; i++)
    {
    m_channels[i] = channels[i];
    }
    }

    // user checked on a "board" or "all channels"
    void ChannelsDlg::onCheckBoard(bool checked)
    {
    QWidget* w = focusWidget();
    int i, j, start, end, board = w ? w->property("board").toInt() : 0;

    start = board > 3 ? 0 : board;
    end = board > 3 ? 4 : board + 1;
    
    for (j = start; j < end; j++)
    {
        for (i = 0; i < 32; i++)
        {
            m_channel[(j * 32) + i]->setChecked(checked);
        }
    }
    

    }

    // save the check boxes to the channel bitmask
    void ChannelsDlg::accept()
    {
    int i, index;
    m_channels[0] = m_channels[1] = m_channels[2] = m_channels[3] = 0;

    for (i = 0; i < 128; i++)
    {
        index = i / 32;
        m_channels[index] = m_channel[i]->isChecked() ? m_channels[index] | 1 << (i % 32) : m_channels[index];
    }
    
    QDialog::accept();
    

    }

    void ChannelsDlg::getChannels(unsigned int* channels)
    {
    int i;
    for (i = 0; i < 4; i++)
    {
    channels[i] = m_channels[i];
    }
    }

    // setup the dialog manually
    int ChannelsDlg::exec()
    {
    QVBoxLayout* vl = new QVBoxLayout;
    QDialogButtonBox* bb;
    QCheckBox* cb;
    QGroupBox* board;
    QGridLayout* gl;
    int i, index, row, ch;

    for (i = 0; i < 4; i++)
    {
        board = new QGroupBox;
        board->setTitle(QString(tr("Board %1")).arg(i + 1));
        gl = new QGridLayout;
    
        for (row = 0; row < 4; row++)
        {
            for (ch = 0; ch < 8; ch++)
            {
                index = (i * 32) + (row * 8) + ch;
                m_channel[index] = new QCheckBox;
                m_channel[index]->setText(QString("Ch: %1").arg(index));
                m_channel[index]->setChecked((m_channels[i] & (1 << ((row * 8) + ch))) != 0);
                gl->addWidget(m_channel[index], row, ch);
            }
        }
    
        board->setLayout(gl);
    
        cb = new QCheckBox;
        cb->setText(QString(tr("Entire Board %1")).arg(i + 1));
        cb->setProperty("board", i);
        cb->setChecked(true);
        connect(cb, SIGNAL(toggled(bool)), this, SLOT(onCheckBoard(bool)));
        vl->addWidget(cb);
        vl->addWidget(board);
    }
    
    cb = new QCheckBox;
    cb->setText(QString(tr("All Channels")));
    cb->setProperty("board", 10);
    cb->setChecked(true);
    connect(cb, SIGNAL(toggled(bool)), this, SLOT(onCheckBoard(bool)));
    vl->addWidget(cb);
    
    // make sure user can use OK & Cancel
    bb = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    connect(bb, SIGNAL(accepted()), this, SLOT(accept()));
    connect(bb, SIGNAL(rejected()), this, SLOT(reject()));
    vl->addWidget(bb);
    
    setLayout(vl);
    resize(400, 400);
    setWindowTitle(tr("Channel Selection"));
    
    return QDialog::exec();
    

    }


  • Qt Champions 2016

    Hi
    It seems that you are missing the includes ?
    C2027: use of undefined type 'QVBoxLayout'
    C2027: use of undefined type 'QGroupBox'

    #include <QGroupBox>
    #include <QVBoxLayout>

    etc



  • Thank you, mrjj.
    You were right! After adding them, the C2027 errors are gone.

    However, My code is still not working because of C4800 and C4251 errors.
    D:\Programes\Qt\5.6.2\5.6\msvc2015_64\include\QtWidgets\qwidget.h:858: warning:
    C4800: 'uint': forcing value to bool 'true' or 'false' (performance warning)

    D:\Programes\Qt\5.6.2\5.6\msvc2015_64\include\QtWidgets\qlayoutitem.h:79: warning:
    C4251: 'QLayoutItem::align': class 'QFlagsQt::AlignmentFlag' needs to have dll-interface to be used by clients of class 'QLayoutItem'


  • Qt Champions 2016

    @gj21kr

    Those are just warning ?

    So you program should run since they are just warnings.

    its impossible to guess at without the real source code line that it comes from. ( not the one inside Qt)
    Its most likely due to Qt4->Qt5 upgrade.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.