【问题标题】:setWindowFilePath didn't work in Qt at allsetWindowFilePath 在 Qt 中根本不起作用
【发布时间】:2011-06-05 04:23:53
【问题描述】:

为什么 setWindowFilePath 不起作用?插槽正在工作。窗口标题不会改变。 我的操作系统是 Windows 7,Qt 编译时支持 wchar_t。

test::test(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
{
  ui.setupUi(this);
  QObject::connect(ui.pushButton, SIGNAL(clicked()), SLOT(Click()));
  setWindowTitle("Title");
}

void test::Click()
{
  setWindowFilePath("file.txt");
}

【问题讨论】:

标签: c++ qt title titlebar


【解决方案1】:

也许你的问题是你在使用setWindowFilePath()之前已经使用了setWindowTitle()。来自docs

如果在任何时候设置了窗口标题,则窗口标题优先显示,而不是文件路径字符串。

编辑:我刚刚尝试使用setWindowFilePath(),并注意到它只有在调用show() 之后才会生效。由于文档中没有提到这一点,它闻起来像一个错误......

编辑:好吧,如果不使用setWindowTitle() 或在调用show() 后调用setWindowFilePath() 不起作用,我不知道你的问题是什么。我已经做了一个工作示例,希望这可以帮助您找到问题:

#include <QApplication>
#include <QMainWindow>
#include <QPushButton>

class MyWindow : public QMainWindow
{
        Q_OBJECT

    public:

        MyWindow()
        {
            QPushButton* b = new QPushButton("Click me", this);
            connect(b, SIGNAL(clicked()), this, SLOT(click()));
        }

    private Q_SLOTS:

        void click()
        {
            setWindowFilePath("file.txt");
        }
};

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);

    MyWindow w;
    w.show();

    return app.exec();
}

#include "main.moc"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-25
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    相关资源
    最近更新 更多