PDA

View Full Version : Align WindowTitle text to the Center in Windows 10



santosh.kumar
4th November 2015, 05:54
Hi

I am using Qt 4.6.4 on Windows. I am using QMainWindow and using setWindowTitle to set the title bar text. But by default OS set it to the left Aligned. Is there function available to Align title bar text to the Center.

Thanks
Santosh

d_stranz
4th November 2015, 22:23
Window decorations are generally controlled by the window manager (i.e. Explorer in Windows) and are outside the control of application software. As far as I know, this cannot be changed through Qt. You may be able to find a Windows OS method that will do it, but I have no idea where to tell you to look.

santosh.kumar
6th November 2015, 02:45
Hi

Is there any alternative or style sheet for QMainWindow or QDialog for Title Bar Text Alignment. I have one Alternative method for Align Text to the Center of MainWindow. Sample Code is below....

void MainWindow::resizeEvent(QResizeEvent *event)
{
QString strTitle("My Application");
QString strGap = QString(" ");

QFont font("Arial",11);
QFontMetrics fontM(font);
int nWid = fontM.width(strTitle);
int nWidSpace = fontM.width(strGap);

int nLengthPixmap = 10;

double STPoint = (double)((double)(this->width()/2-nLengthPixmap) - (double)nWid/2);
double widthOfSpace = (double)nWidSpace;

QString strG = QString(" ");
double tmpWidth = 0;

for(;(tmpWidth+widthOfSpace < STPoint);)
{
strG += QString(" ");
tmpWidth += widthOfSpace;
}
QString strFinal = strG + strTitle;// + strG;
setWindowTitle(strFinal);
}


I have customize resizeEvent, it will set the title bar to the center. But problem is that, Due to Center Alignment text taskbar only show Application Icon followed by ....., I have attached the ScreenShot of My Application, Due to Center Alignment, Text shift also from TaskBar..

Can you please give the solution that What is missing by me, After Center alignment of Window Title Text, Bottom Task Bar should also show Application's window title as in screenshot of other program..

Thanks
Santosh Kumar Singh

d_stranz
6th November 2015, 15:53
the solution that What is missing by me

What you are "missing" is the understanding that your code cannot override what the Windows OS is doing with the alignment of the window title. Your code tries to fake it by left-padding the title with extra spaces. This isn't alignment, it is changing the title to make it longer. And the Windows OS is doing exactly what it is supposed to do - it is taking your title and making a task bar button out of it. If you give the window a title that is too long to fit into the task bar button, then you see an apparently blank label because you've left padded it with spaces.