[英]How to change window title and central widget in Qt?
嗨,我在Qt中更改窗口標題和中央窗口小部件時遇到問題。 有MainWindow:
class MainWindow : public QMainWindow
{
// (...)
QStackedWidget* widgets;
Quiz* widget1, *widget2;
}
並且有一個類測驗:
class Quiz : public QWidget
{
public slots:
void myClicked();
}
我想在點擊按鈕后更改MainWindow標題,該按鈕是測驗的一個元素(並且它與插槽myClicked連接)。
void Quiz::myClicked()
{
static_cast<MainWindow>(parent).myFunction();
}
void MainWindow::myFunction()
{
widget2 = new Quiz(this,2);
widgets->addWidget(widget2);
std::cout<<"current wdgt: " << widgets->currentIndex() << std::endl; // shows: 0
widgets->setCurrentWidget(widget2);
std::cout<<"current wdgt " << widgets->currentIndex() << std::endl; // shows: 1
setWindowTitle("newTitle");
std::cout<<"Title is " << windowTitle().toStdString() << std::endl;
}
所以widgets-> currentIndex顯示了新窗口小部件的索引,但窗口中沒有任何更改。 窗口標題也是同樣的問題 - 方法windowTitle()返回新標題,但標題欄上的標題是舊的。 為什么? 如果我在Quiz :: myClicked中更改標題:
parent->setWindowTitle("newTitle");
有用! 為什么它有多奇怪? 請幫忙。
標題欄顯示MainWindow的標題。 您的測驗小部件位於MainWindow的“內部”,因此不會顯示其標題。
如果要更改標題欄文本,則必須調用MainWindow::setWindowTitle()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.