what qwidget subclass?



  • 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?



  • @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));
    }
    


  • 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!


  • Qt Champions 2017

    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



  • 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


  • Qt Champions 2017

    @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.



  • 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?


  • Qt Champions 2017

    @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.



  • @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


  • Qt Champions 2017

    @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 ?



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


  • Qt Champions 2017

    @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 :)



  • @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


  • Qt Champions 2017

    @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.



  • @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


  • Qt Champions 2017

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



  • @mrjj

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


  • Qt Champions 2017

    @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");
    
    


  • @mrjj

    yes i've tried everything

    okay, thanks anyways


  • Qt Champions 2017

    @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.



  • @mrjj

    it's really just a path

    i even tried to QDir::toNativeSeparators(). nothing works


  • Lifetime Qt Champion

    Hi,

    Since you are using Qt, why not use the Unix notation ? Qt handles the conversion when it calls the related platform specific functions.

    So it should be C:/Users/user/image.png



  • @SGaist

    i've tried that too, it doesn't work


  • Lifetime Qt Champion

    Does that file have any permission issues ?
    Are you 100% sure that it's there ?
    Are you loading it from your own user account ?
    Any special char in the path ? i.e is user really the account name ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.