Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can't declare QPixmap as global variable
QtWS25 Last Chance

Can't declare QPixmap as global variable

Scheduled Pinned Locked Moved General and Desktop
qpixmapglobalvariable
2 Posts 2 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rphysx
    wrote on last edited by Rphysx
    #1

    While trying to create a QPixmap as a global variable, i'm getting the following LINK errors:

    •main.obj:-1: error: LNK2001: unresolved external symbol "class QPixmap * _corner" (?_corner@@3PEAVQPixmap@@EA)
    •mainwindow.obj:-1: error: LNK2001: unresolved external symbol "class QPixmap * _corner" (?_corner@@3PEAVQPixmap@@EA)

    Here's MainWindow.h:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QtGui>
    #include <QtCore>
    #include <QGraphicsDropShadowEffect>
    
    extern QPixmap* _corner;
    
    namespace Ui {
    class MainWindow;
    }
    class MainWindow : public QMainWindow
    {
    Q_OBJECT
    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    private:
    Ui::MainWindow *ui;
    protected:
    void paintEvent(QPaintEvent *e);
    };
    #endif // MAINWINDOW_H
    

    and main.cpp:

    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    
    _corner->load("C:\\Users\\Domenico\\Desktop\\corner.png");
    
    w.show();
    return a.exec();
    }
    
    A 1 Reply Last reply
    0
    • R Rphysx

      While trying to create a QPixmap as a global variable, i'm getting the following LINK errors:

      •main.obj:-1: error: LNK2001: unresolved external symbol "class QPixmap * _corner" (?_corner@@3PEAVQPixmap@@EA)
      •mainwindow.obj:-1: error: LNK2001: unresolved external symbol "class QPixmap * _corner" (?_corner@@3PEAVQPixmap@@EA)

      Here's MainWindow.h:

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      #include <QtGui>
      #include <QtCore>
      #include <QGraphicsDropShadowEffect>
      
      extern QPixmap* _corner;
      
      namespace Ui {
      class MainWindow;
      }
      class MainWindow : public QMainWindow
      {
      Q_OBJECT
      public:
      explicit MainWindow(QWidget *parent = 0);
      ~MainWindow();
      private:
      Ui::MainWindow *ui;
      protected:
      void paintEvent(QPaintEvent *e);
      };
      #endif // MAINWINDOW_H
      

      and main.cpp:

      #include "mainwindow.h"
      #include <QApplication>
      
      int main(int argc, char *argv[])
      {
      QApplication a(argc, argv);
      MainWindow w;
      
      _corner->load("C:\\Users\\Domenico\\Desktop\\corner.png");
      
      w.show();
      return a.exec();
      }
      
      A Offline
      A Offline
      alex_malyu
      wrote on last edited by alex_malyu
      #2

      This seems a misunderstanding of language basics.
      Global and extern meaning are different.

      I would highly advice reading the book.

      But briefly:
      In such context extern means that you want to access variable _corner which is declared and instantiated in other module.
      Since it is not true you get a link error.

      At the same time I would highly advice against global variables in C++.
      Using such leads to code which is highly difficult to maintain.
      If you really need something accessible globally you may either subclass QApplication or use singleton pattern
      or at least use static class members .

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved