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. what qwidget subclass?
Qt 6.11 is out! See what's new in the release blog

what qwidget subclass?

Scheduled Pinned Locked Moved Solved General and Desktop
24 Posts 4 Posters 10.4k Views 3 Watching
  • 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.
  • U Offline
    U Offline
    user4592357
    wrote on last edited by
    #1

    i need to create some kind of widget and i want it to act this way: it should contain an image, be clickable and also, when right button of mouse is clicked, i want a menu to be displayed. something like this:alt text
    what kind of widget should i use?

    Taz742T 1 Reply Last reply
    1
    • U user4592357

      i need to create some kind of widget and i want it to act this way: it should contain an image, be clickable and also, when right button of mouse is clicked, i want a menu to be displayed. something like this:alt text
      what kind of widget should i use?

      Taz742T Offline
      Taz742T Offline
      Taz742
      wrote on last edited by Taz742
      #2

      @user4592357
      http://doc.qt.io/qt-4.8/qwidget.html#customContextMenuRequested

      For Example (tableWidget):

      //constructor
      ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);
      connect(ui->tableWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(SLOTCustomMenu(QPoint)));
      
      //on_SLOT
      void MainWindow::SLOTCustomMenu(QPoint pos)
      {
          QMenu myMenu;
      
          QAction *act1 = myMenu.addAction("&foo", this, SLOT(SLOTFoo()));
          act1->setIcon(QIcon(":/SMSicons/foo.png"));
      
          QAction *act2 = myMenu.addAction("&bar", this, SLOT(SLOTBar()));
          act2->setIcon(QIcon(":/SMSicons/bar.png"));
      
          myMenu.exec(ui->tableWidget->mapToGlobal(pos));
      }
      

      Do what you want.

      1 Reply Last reply
      3
      • U Offline
        U Offline
        user4592357
        wrote on last edited by user4592357
        #3

        i need a widget that will have borders and an icon inside it (in the center of the widget), so i decided to use QFrame since it has a QFrame::Box property, and i'm drawing a pixmap on it but something is wrong. here's my code:

        class myWidget : public QFrame {
        
            Q_OBJECT
        
        public:
        
           myWidget(const QString &iconPath, QWidget *parent = nullptr);
        
            virtual ~myWidget() override {}
        
        protected:
        
            virtual void paintEvent(QPaintEvent *event) override;
        
        private:
        
            QPixmap pixmap;
        
        };
        

        implementation:

        myWidget::myWidget(const QString &iconPath, QWidget *parent /* = nullptr */) : QFrame(parent) {
        
            setAttribute(Qt::WA_TranslucentBackground);
            setFixedSize(140, 180);
            setFrameStyle(QFrame::Box);
        
            pixmap.load(iconPath);
        }
        
        void myWidget::paintEvent(QPaintEvent */* event */) {
            QPainter painter(this);
            painter.drawPixmap(0, 0, width(), height() / 2, pixmap);
        }
        

        not only i don't get borders drawn around the widget, but also no icon loaded (actually, when i remove the paintEvent(), the border is drawn (because of QFrame::Box) but not with it).

        this is how i use the class:

        auto layout = new QVBoxLayout;
        layout->addWidget(new myWidget(icon-full-path, this));
        // etc...
        

        the icon-full-path contains "\\"s instead of "\"s, just in case, but no icon is displayed

        i've also tried using all "/"s but no success

        please help!

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          Hi
          You must call the base class's paintEvent so it can draw the border

          void myWidget::paintEvent(QPaintEvent event ) {
          QFrame::paintEvent(event); // let base class paint
          ....

          About the icon.
          Are you use external file ?
          Try with resource file and that syntax
          Like ":/SMSicons/foo.png"
          http://doc.qt.io/qt-5/resources.html

          1 Reply Last reply
          3
          • U Offline
            U Offline
            user4592357
            wrote on last edited by
            #5

            but QPixmap can load from QString only, that's why i changed it from QIcon.

            and yes, i'll need to load external files too so that's another problem

            mrjjM 1 Reply Last reply
            0
            • U user4592357

              but QPixmap can load from QString only, that's why i changed it from QIcon.

              and yes, i'll need to load external files too so that's another problem

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @user4592357
              Hi
              If you need to load external files, where will they be placed ?
              The user will point to them ?

              Using a Resources file, includes the images in the exe file and can be loaded with QString and pixmap.

              1 Reply Last reply
              1
              • U Offline
                U Offline
                user4592357
                wrote on last edited by user4592357
                #7

                the user will provide a path to the image, and i should go get the image from the path and display it in the widget

                EDIT: i was able to draw using an entry from qrc but what should i do with that user provided icon paths?

                mrjjM 1 Reply Last reply
                1
                • U user4592357

                  the user will provide a path to the image, and i should go get the image from the path and display it in the widget

                  EDIT: i was able to draw using an entry from qrc but what should i do with that user provided icon paths?

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @user4592357

                  Ok, sounds like a normal file open dialog
                  http://www.codebind.com/cpp-tutorial/qt-tutorial/qt-tutorials-beginners-qfiledialog-getopenfilename-example/

                  Then load it via pixmap. Should just work.

                  U 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @user4592357

                    Ok, sounds like a normal file open dialog
                    http://www.codebind.com/cpp-tutorial/qt-tutorial/qt-tutorials-beginners-qfiledialog-getopenfilename-example/

                    Then load it via pixmap. Should just work.

                    U Offline
                    U Offline
                    user4592357
                    wrote on last edited by
                    #9

                    @mrjj

                    no i don't need to show a dialog, i just need to go and grab the image located in the user-specified path

                    mrjjM 1 Reply Last reply
                    0
                    • U user4592357

                      @mrjj

                      no i don't need to show a dialog, i just need to go and grab the image located in the user-specified path

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #10

                      @user4592357
                      Ok, i see no reason it should not work if the paths are fully qualified and
                      valid.

                      I would test that
                      qDebug() << "loading: " pixmap.load(iconPath);
                      says true
                      also i would try with

                      #include <QFileInfo>
                      
                      bool fileExists(QString path) {
                          QFileInfo check_file(path);
                          // check if file exists and if yes: Is it really a file and no directory?
                          return check_file.exists() && check_file.isFile();
                      }
                      
                      and check iconPath where u try to load the image.
                      

                      Also where does icon-full-path come from ?
                      Is it in the app or do you load from text file ?

                      1 Reply Last reply
                      1
                      • U Offline
                        U Offline
                        user4592357
                        wrote on last edited by
                        #11

                        thanks. i'll read a text file for that, but why does that matter?

                        mrjjM 1 Reply Last reply
                        0
                        • U user4592357

                          thanks. i'll read a text file for that, but why does that matter?

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @user4592357
                          Well if inside the app you must escape

                          c:\\xxx\\cyy\\
                          

                          But that is not valid if from text file so just asking to try to guess reason that is not shown :)

                          U 1 Reply Last reply
                          1
                          • mrjjM mrjj

                            @user4592357
                            Well if inside the app you must escape

                            c:\\xxx\\cyy\\
                            

                            But that is not valid if from text file so just asking to try to guess reason that is not shown :)

                            U Offline
                            U Offline
                            user4592357
                            wrote on last edited by
                            #13

                            @mrjj

                            QFile::exists() gives false when i use a full path for the image (like, C:\Users\user\image.png). but the file sure exists. i've tried using \\ instead of \, also / but not much success. with resource system, however, everything is okay

                            mrjjM 1 Reply Last reply
                            0
                            • U user4592357

                              @mrjj

                              QFile::exists() gives false when i use a full path for the image (like, C:\Users\user\image.png). but the file sure exists. i've tried using \\ instead of \, also / but not much success. with resource system, however, everything is okay

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @user4592357
                              if you take one of the paths from the file
                              and start paintbrush, select file->open
                              and paste the path into the filename area. and then press open.
                              will it then open ?

                              it sounds really, really wrong if QFile::exists() says false.

                              U 1 Reply Last reply
                              0
                              • mrjjM mrjj

                                @user4592357
                                if you take one of the paths from the file
                                and start paintbrush, select file->open
                                and paste the path into the filename area. and then press open.
                                will it then open ?

                                it sounds really, really wrong if QFile::exists() says false.

                                U Offline
                                U Offline
                                user4592357
                                wrote on last edited by
                                #15

                                @mrjj

                                yes it opens.

                                this is weird, really.
                                this works:

                                QFileInfo file("C:\Users\user\image.png");
                                setIcon(file.filePath());
                                

                                but when i get the SAME line from a file and set the icon to it... it doesn't work

                                mrjjM 1 Reply Last reply
                                0
                                • U user4592357

                                  @mrjj

                                  yes it opens.

                                  this is weird, really.
                                  this works:

                                  QFileInfo file("C:\Users\user\image.png");
                                  setIcon(file.filePath());
                                  

                                  but when i get the SAME line from a file and set the icon to it... it doesn't work

                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  Hi
                                  Did you try to qDebug() << file.filePath();
                                  to see if anything still 100% like the one in file?

                                  U 1 Reply Last reply
                                  1
                                  • mrjjM mrjj

                                    Hi
                                    Did you try to qDebug() << file.filePath();
                                    to see if anything still 100% like the one in file?

                                    U Offline
                                    U Offline
                                    user4592357
                                    wrote on last edited by
                                    #17

                                    @mrjj

                                    yes they're the same, actually i've copied them

                                    mrjjM 1 Reply Last reply
                                    0
                                    • U user4592357

                                      @mrjj

                                      yes they're the same, actually i've copied them

                                      mrjjM Offline
                                      mrjjM Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on last edited by mrjj
                                      #18

                                      @user4592357
                                      Really cannot guess then.
                                      If path in text file truely are valid syntax, it should just work.
                                      and you are 100% sure you escape it ?
                                      ( which is needed when inside the code, but from text file)

                                      setIcon("C:\\Users\\user\\image.png");
                                      
                                      
                                      U 1 Reply Last reply
                                      0
                                      • mrjjM mrjj

                                        @user4592357
                                        Really cannot guess then.
                                        If path in text file truely are valid syntax, it should just work.
                                        and you are 100% sure you escape it ?
                                        ( which is needed when inside the code, but from text file)

                                        setIcon("C:\\Users\\user\\image.png");
                                        
                                        
                                        U Offline
                                        U Offline
                                        user4592357
                                        wrote on last edited by
                                        #19

                                        @mrjj

                                        yes i've tried everything

                                        okay, thanks anyways

                                        mrjjM 1 Reply Last reply
                                        0
                                        • U user4592357

                                          @mrjj

                                          yes i've tried everything

                                          okay, thanks anyways

                                          mrjjM Offline
                                          mrjjM Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #20

                                          @user4592357
                                          I have done it many times so I do
                                          wonder what you do that is
                                          different or if it really just is a path issue.

                                          U 1 Reply Last reply
                                          0

                                          • Login

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