QStatusbar priority possible??



  • Hi,

    i wonder if it is possible to have priority messages at the QStatusbar.
    My program can open files, so there is a message "open file..." and it displays some import status "loading textures..", "process vertex.."
    So if it is a small file, the open file message is quick replaced by loading texutres, process vertex, done,...

    But if there are problems at the import i want to show a message for longer and maybe yellow/red painted.
    So let's say there are conflicts, the message "Warning..." can not be overwritten by normal messages, but by other warnings or errors. After the time went out, the message disappear and everything can be displayed again.


  • Moderators

    @QT-static-prgm
    you can subclass QStatusBar and implement this logic yourself (untested):

    enum MessageType {
       NormalMessageType,
       WarningMessageType,
       ErrorMessageType
    };
    
    void MyStatusBar::showMessage( const QString & message, int timeout = 0, MessageType type = NormalMessageType )
    {
             QString msg;
             switch( type )
             {
                    case NormalMessageType:
                                  if( mCurrentMessageTypeDisplayed != NormalMessageType )
                                       return;
                                  msg = message;
                         break;
                    case WarningMessageType:
                                 msg = QString("<font color=\"yellow\">%1</font>").arg( message );
                          break;
                    case ....
            }
             
            mCurrentMessageTypeDisplayed = type;
            QStatusBar::showMessage( msg, timeout );
    }
    
    //connect this slot to QStatusBar's messageChanged() signal
    void MyStatusBar::onMessageChanged( const QString & message )
    {
            if( mCurrentMessageTypeDisplayed != NormalMessageType && message.isNull() )
                 mCurrentMessageTypeDisplayed = NormalMessageType;
    }
    

    I am not quite sure if the QStatusBar supports rich-text (regarding text coloring).

    Probably it's easier to add a QLabel as permanent widget and set the error and warning messages onto it. As you like.


  • Moderators

    QStatusBar doesn't support rich text. For whole message formatting you can get away with stylesheets though:

    case WarningMessageType:
         setStyleSheet("color: yellow");
         msg = message;
         break;
    

    and you should probably use message.isEmpty() instead of message.isNull().



  • now i have so many solutions, thank you both :D
    i integrated it into my MainWindow class that way:

    void MainWindow::showMessage(QString message, int severity)
    {
    	if (severity < m_curSeverity)
    		return;
    
    	m_curSeverity = severity;
    	int time(0);
    	QPalette palette;
    
    	switch (severity)
    	{
    	case 1:
    		time = 3000;
    		palette.setColor(QPalette::WindowText, Qt::darkYellow);
    		break;
    	case 2:
    		time = 3000;
    		palette.setColor(QPalette::WindowText, Qt::red);
    		break;
    	case 0:
    	default:
    		time = 2000;
    		palette.setColor(QPalette::WindowText, Qt::black);
    		break;
    	}
    
    	ui->statusBar->setPalette(palette);
    	ui->statusBar->showMessage(message, time);
    }
    

  • Moderators

    @Chris-Kawa said in QStatusbar priority possible??:

    and you should probably use message.isEmpty() instead of message.isNull().

    normally i would also do so, but the docs say so, so i added it also this way.
    Thus both methods do the trick here.


Log in to reply
 

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