Qt World Summit: Register Today!

Help to get Network Card Mac address

  • Hi I have the following code, but it is not working,
    I am only beginner.
    Error I receive is "Internal Error during IMAGE::Pass1

    I wish to get a string that I can pass into a label or somewhere.
    I assume my problem is that I copied the code but it needs to be declared somewhere
    and also that my QString getMacAddress() function I am using incorrect.

    I have edited the .pro file with the first line to look like this:
    QT += core gui network

    Please help me

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "QString"
    #include "QtNetwork/QNetworkInterface"
    #include "QtNetwork/QtNetwork"
    MainWindow::MainWindow(QWidget *parent) :
        ui(new Ui::MainWindow)
        delete ui;
    QString getMacAddress()
        foreach(QNetworkInterface netInterface, QNetworkInterface::allInterfaces())
            // Return only the first non-loopback MAC Address
            if (!(netInterface.flags() & QNetworkInterface::IsLoopBack))
                return netInterface.hardwareAddress();
        return QString();

  • Lifetime Qt Champion

    @Almero When do you get this error message?

  • I had a weird error now, so I deleted the project and started a new one. I left the .pro file exactly as is.
    I now get this error.

    Error is on windows machine


  • Lifetime Qt Champion

    @Almero Please do a complete rebuild (delete build directory, run qmake and build).

  • I have run qmake, rebuild all etc but the error is still the same.

    Is "QString getMacAddress()" function even at the correct place?
    does it not need to be directly after "ui->setupUi(this);" for example?

    using QT5.9.2 MSVC 2015

  • Lifetime Qt Champion

    @Almero It doesn't matter where it is for your error message. The problem is: your app is not linked against the Qt network module, so linker cannot find the symbols. Are you sure you have QT += network in your pro file? And did you delete the build directory before running qmake as I suggested?

  • @jsulm

    I have added QT += network as a seperate line in the .pro file and it now works :)
    Thank you so much

Log in to reply