qt creator error: LNK 2019: unresolved external symbol “__declspec(dllimport) public: void__thiscall

StackOverflow https://stackoverflow.com/questions/17257741

سؤال

I would like to know why I am getting these errors.I am using Qt 5.0.2 and msvc2010 compiler. It runs normally when i delete the blah function. I'm not an expert programmer at all, please answer me as if i dont know anything, thank you!

Error: http://puu.sh/3m6Qr.png

My codes below:

.pro

QT       += core gui
QT       += widgets
QT       += network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = guangdong
TEMPLATE = app

SOURCES += main.cpp\
        login.cpp

HEADERS  += login.h

FORMS    += login.ui

login.cpp

#include "login.h"
#include "ui_login.h"
#include <QtSql/QSql>
#include <QtSql/QSqlDatabase>
#include <QDebug>
#include <Query.h>
#include <QString>
#include <QtSql/QSqlQuery>
#include <QtNetwork/QNetworkInterface>

login::login(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::login)
{
    ui->setupUi(this);
    blah();
}

login::~login()
{
    delete ui;
}
void login::blah()
{
   // QSqlQuery query;
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("blah");
    db.setDatabaseName("blah");
    db.setUserName("blah");
    db.setPassword("blah");
    bool ok = db.open();

    if ( ok ) {
        ui->label->setText("databaseopen");
        db.close();
    }
    else
        ui->label->setText("Error opening");
}

main.cpp

#include "login.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    login w;
    w.show();

    return a.exec(&#41;;
}

EDIT: I added Qt += sql and #include but now i get this error. Error: http://puu.sh/3maq2.png

هل كانت مفيدة؟

المحلول

The error messages say the linker cannot find the external symbols defined in the header <QtSql/QSqlQuery>. You need to link against QtSql library/module: http://qt-project.org/doc/qt-5.0/qtsql/qtsql-index.html

QT       += core gui
QT       += widgets
QT       += network
QT       += sql

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = guangdong
TEMPLATE = app

SOURCES += main.cpp\
        login.cpp

HEADERS  += login.h

FORMS    += login.ui

نصائح أخرى

Run qmake after you update your .pro file and also you are adding the widgets module twice and that can also cause troubles:

QT       += core gui
//QT       += widgets delete this line Qt4 doesn't have widgets and for Qt 5 the widgets are added at the last line
QT       += network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

for more complicated stuff you can use *= operator to ensure that a value is added to the list of values in a variable only once.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top