0

I have classes, ImageLabel, and ChoiceLabel. ChoiceLabel inherits from ImageLabel. When I try to make a new ChoiceLabel, I get an Unresolved External Symbol error. Here are the relevant files:

imagelabel.h

#ifndef IMAGELABEL_H
#define IMAGELABEL_H

#include <QLabel>

class QPixmap;
class QStringList;
class QFileDialog;
class QResizeEvent;

class ImageLabel : public QLabel
{
    Q_OBJECT
public:
    explicit ImageLabel(QWidget *parent = 0);

protected:
    virtual int heightForWidth( int width ) const;
    virtual QSize sizeHint() const;
    QPixmap scaledPixmap() const;
    void setPixmap ( const QPixmap & );
    void resizeEvent(QResizeEvent *);

private:
    QPixmap pix;
};

#endif // IMAGELABEL_H

choicelabel.h

#ifndef CHOICELABEL_H
#define CHOICELABEL_H

class QStringList;

class ChoiceLabel : public ImageLabel
{
    Q_OBJECT
public:
    ChoiceLabel();
private:
    QStringList *imageFiles;
    void mousePressEvent(QMouseEvent *);
    void keyPressEvent(QKeyEvent *);
    void focusInEvent(QFocusEvent *);
    void focusOutEvent(QFocusEvent *);
};

#endif // CHOICELABEL_H

mainwindow.cpp

#include "mainwindow.h"
#include "imagelabel.h"
#include "choicelabel.h"
#include <QGridLayout>

#include <qDebug>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{

    choiceLabelA = new ChoiceLabel; //the problem occurs here
    resultLabel = new ImageLabel;

    centralWidget = new QWidget(this);
    this->setCentralWidget(centralWidget);

    gridLayout = new QGridLayout(centralWidget);
    gridLayout->addWidget(resultLabel);
}

EDIT:

imagelabel.cpp

#include "imagelabel.h"
#include <QDebug>

ImageLabel::ImageLabel(QWidget *parent) :
    QLabel(parent)
{
    setMinimumSize(250,250);
    setAlignment(Qt::AlignCenter | Qt::AlignCenter);
    setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);

    QPixmap initialPixmap(250, 250);
    initialPixmap.fill(Qt::black);
    setPixmap(initialPixmap);
}

void ImageLabel::setPixmap ( const QPixmap & p)
{
    pix = p;
    QLabel::setPixmap(scaledPixmap());
}

QPixmap ImageLabel::scaledPixmap() const
{
    return pix.scaled(this->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
}

int ImageLabel::heightForWidth( int width ) const
{
    return pix.isNull() ? this->height() : ((qreal)pix.height()*width)/pix.width();
}

QSize ImageLabel::sizeHint() const
{
    int w = this->width();
    return QSize( w, heightForWidth(w) );
}

void ImageLabel::resizeEvent(QResizeEvent *)
{
    if(!pix.isNull())
        QLabel::setPixmap(scaledPixmap());
}

choicelabel.cpp

#include "choicelabel.h"
#include <QStringList>
#include <QFileDialog>
#include <QFrame>

ChoiceLabel::ChoiceLabel():
    imageFiles()
{
    setFocusPolicy(Qt::StrongFocus);
}

void ChoiceLabel::mousePressEvent(QMouseEvent *)
{
    QFileDialog dialog(this);
    dialog.setFileMode(QFileDialog::ExistingFiles);
    dialog.setNameFilter("Images (*.png *.jpg)");
    dialog.setDirectory("C:/Users/FrankFritz/Desktop/qt/uglypictures");

    if (dialog.exec()){
        imageFiles = dialog.selectedFiles();
        QPixmap pixmap = imageFiles->first();
        setPixmap(pixmap);
    }
}

void ChoiceLabel::keyPressEvent(QKeyEvent *ev)
{
    static int index(0);
    if (ev->key() == Qt::Key_Right){
        index = (index + 1) % imageFiles->length();
        setPixmap( QPixmap(imageFiles->at(index)) );
    }
    if (ev->key() == Qt::Key_Left){
        if (index == 0){
            index = imageFiles->length() - 1;
        }
        else{
            index = (index - 1) % imageFiles->length();
        }
        setPixmap( QPixmap(imageFiles->at(index)) );
    }
}

void ChoiceLabel::focusInEvent(QFocusEvent *)
{
    setFrameStyle(QFrame::StyledPanel);
}

void ChoiceLabel::focusOutEvent(QFocusEvent *)
{
    setFrameStyle(QFrame::NoFrame);
}
  • 1
    *"I get an Unresolved External Symbol error"* - Don't you think the **full** error message might be relevant? – IInspectable Oct 22 '16 at 18:31
  • Most likely, you missed the definition of one (or more) of these functions `ChoiceLabel::ChoiceLabel()`/`ChoiceLabel::mousePressEvent()`/`ChoiceLabel::keyPressEvent()`/`ChoiceLabel::focusInEvent()`/`ChoiceLabel::focusOutEvent()` – Mike Oct 22 '16 at 18:34
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Mike Oct 22 '16 at 18:36
  • post your imagelabel and choice label cpp files – Pavan Chandaka Oct 22 '16 at 21:07
  • Pavan, I have just edited the post. – Frank Fritz the Blitz Oct 22 '16 at 21:12

1 Answers1

1

Most probably

ChoiceLabel::ChoiceLabel():
    imageFiles()
{
    setFocusPolicy(Qt::StrongFocus);
}


ChoiceLabel::ChoiceLabel(QWidget *parent): ImageLabel(parent)
{
    setFocusPolicy(Qt::StrongFocus);
}

Forgot to call the base class' constructor. Also forgot to specify a parent QWidget * for ChoiceLabel in its own constructor.

See What are the rules for calling the superclass constructor? for reference.

Community
  • 1
  • 1
iksemyonov
  • 4,106
  • 1
  • 22
  • 42