Button on MS Windows taskbar



  • Hi,
    I'm newbie in QT, and i need some help. Please tell me how can i make button which will be in the foreground and which i can move by pressed (longer click). For example i want to set up this button on MS Windows taskbar and have this on hand, like Messenger cloud App on Android.
    PS My english is poor so i'm sorry for grammar :)


  • Moderators

    @zumma Hi, welcome to the Qt forum! Do you mean a System Tray Icon?


  • Qt Champions 2016

    Hi and welcome
    Can you provide more info ? what sort of button ?
    a tray button?

    http://doc.qt.io/qt-5/qsystemtrayicon.html



  • If instead you want to hadle the taskbar icon in windows (left part of @mrjj picture) you can use the winextras module http://doc.qt.io/qt-5/qtwinextras-module.html



  • Ok i forgot change topic :)
    So my idea is ... Round button (picture) somewhere on screen. It should be possible to move and i want to have this at hand ( i don't know if it's possible ), because after click i want to play favorite short .mp3 or after longer click i want to see more .mp3 to choose. System Tray Icon will be good to more options, for example (settings, folder with .mp3). It's project to school on multimedia board. It's must be easy to use. So tell me the best way to do this. Maybe u have better solution.
    I don't know how can i attach picture in post :( but i think you know what i mean :)


  • Moderators

    @zumma said:

    I don't know how can i attach picture in post

    Upload the picture to https://postimage.org/ and then insert it here with ![alternate text](url).



  • picture
    from gimp


  • Moderators

    @zumma Have a look at this example: http://doc.qt.io/qt-5/qtwidgets-widgets-shapedclock-example.html. Should provide you with a good starting point.



  • @Wieland It's look good. I think it will be good for me. Early answers about system try icon will help me too. Thanks all. Maybe someone have one idea more :) so I'll wait with the closing topic.



  • Ok your idea was great :)
    Now i have problem with connect signal and slot. I don't know what is wrong. I spent four hour on this and nothing. I think this is little mistake but i don't know where :) Problem is described in comment in code.
    Next question is ... instruction "foreach" is good for this menu? It's working fine but maybe is better way to do it. Here i want to add name of files to menu. I will change this slot on mediaplayer - > play.
    It's my first project in QT so be understanding.

    ShapedClock::ShapedClock(QWidget *parent)
        : QWidget(parent, Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowStaysOnTopHint)
    {
        QSettings setting("MyApp","mysettings");                                            
                setting.beginGroup("SettingsWindow");                                       
    
        QDir dir(setting.value("sciezka","C:/").toString());                               
    
                setting.endGroup();                                                        
    
        int index = 0;
    
        foreach (QFileInfo var, dir.entryInfoList())                                        
        {                                                                                
            if(var.isFile())
            {
                index++;
                QString aaa = QString::number(index);
                
                QAction *exitAction = new QAction((aaa+ ". " + var.baseName()), this);       
                connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
                addAction(exitAction);
            }
        }
        QAction *exitAction = new QAction(tr("E&xit"), this);
        connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
        addAction(exitAction);
    
    //i can't open new window "settings" ->  error: No such slot shapedClock::OpenSettings()
        QAction *awer = new QAction(tr("S&ettings"), this);
        connect(awer, SIGNAL(triggered()), this, SLOT(OpenSettings()));         
        addAction(awer);                                                       
    /////
    
        setContextMenuPolicy(Qt::ActionsContextMenu);
        setToolTip(tr("abcdefgh\n"
                      "abcdefgh"));
        setWindowTitle(tr("nnnnnnnn"));
    }
    
    // ........
    // ........
    // ........
    
    void ShapedClock::OpenSettings()             // OpenSettings is in protected group in ShapedClock.h
    {
        SettingsDialog settingsDialog;
        settingsDialog.setModal(true);
        settingsDialog.exec();
    }
    


  • @zumma

    OpenSettings() should not be under protected:

    public slots:
    void OpenSettings() ;

    All signals must declare under signals and slots under slots.

    Check this link


  • Moderators

    Btw: You should use the new connect syntax as it has better compile-time checks:

    connect(exitAction, &QAction::triggered, qApp, &QApplication::quit);
    


  • Again you are very helpful. I searched network about my new problem and nothing.

    FilesDialog::FilesDialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::FilesDialog)
    {
        ui->setupUi(this);                                                                 
    
        QSettings setting("MyApp","mysettings");                   // load settings from QSettings 
                setting.beginGroup("SettingsWindow");                                       
    
                      QDir dir( setting.value("path","C:/").toString() );               
                      QListWidgetItem fav ( setting.value("favorite").toString() );
    
                setting.endGroup();                                 // end of loading
    
        foreach (QFileInfo var, dir.entryInfoList())                // additems to QListWidget
        {                                                                                   
            if(var.isFile())                                                               
                ui->listWidget_files->addItem(var.fileName());
        }                                                                             
        
        ui->listWidget_files->setCurrentItem(&fav);                    // I think here is mistake or 
        ui->listWidget_files->currentItem()->setIcon(QIcon(":/pictures/button.png"));    // here
    }
    

    I want to load "favorite"(It's name of item) from settings and set icon item in QListWidget.
    Compilation of the project was succesfully. But when i open this window i get error and no responses.


  • Lifetime Qt Champion

    Hi,

    Your fav object is allocated on the stack so at the end of the function it will be destroyed.



  • I think fav is in one function. When i use qQebug() i see in console "1", "-1" , and i dont see "2" because here is error. I dont know why i got "-1" when i indicate on second item.

        ui->listWidget_files->setCurrentItem(&ulub);
        qDebug() << "1";     // work
    
        qDebug() << ui->listWidget_files->currentRow();    // -1
    
        ui->listWidget_files->currentItem()->setIcon(QIcon(":/pictures/button.png"));     
        qDebug() << "2";
    

  • Moderators

    @zumma As @SGaist said: you pass pointer to a local variable which is destroyed as soon as FilesDialog() finishes:

    FilesDialog::FilesDialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::FilesDialog)
    {
    ...
        QListWidgetItem fav ( setting.value("favorite").toString() ); // fav is local variable and only exists in FilesDialog()
    ...
        ui->listWidget_files->setCurrentItem(&fav); // Here you pass a pointer
    ...
    } // Here  fav is destoyed and the pointer you passed to setCurrentItem becomes invalid
    

  • Moderators

    @zumma Put a break point at

    ui->listWidget_files->currentItem()->setIcon(QIcon(":/pictures/button.png"));
    

    and check whether it really crash at that line.


  • Lifetime Qt Champion

    To add to @jsulm, you should check that the item returned by currentItem is not null before doing anything with it.


Log in to reply
 

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