Qt中可调整大小和可最大化的窗口

时间:2012-05-17 07:44:08

标签: c++ qt qt4

我有一个使用Qt打开窗口的c ++程序,但窗口不可调整大小并且缺少最大化按钮。
大多数代码都是从教程中复制的 见:http://zetcode.com/tutorials/qt4tutorial/firstprograms/
这是正常的吗?这是我的代码:

#include <QApplication>
#include <QDesktopWidget>
#include <QWidget>
#include <QIcon>
#include <QPushButton>
#include <QTextEdit>

using namespace std;

class Frame : public QWidget {
    public: Frame(QWidget *parent = 0);
};

void center(QWidget *widget, int w, int h) {
    int x, y;
    int screenWidth;
    int screenHeight;

    QDesktopWidget *desktop = QApplication::desktop();

    screenWidth = desktop->width();
    screenHeight = desktop->height();

    x = (screenWidth - w) / 2;
    y = (screenHeight - h) / 2;

    widget->move( x, y );
}

Frame::Frame(QWidget *parent) : QWidget(parent) {
    int WIDTH = 250;
    int HEIGHT = 150;

    setFixedSize(WIDTH, HEIGHT);

    QTextEdit *edit = new QTextEdit(this);
    edit -> setGeometry(5, 5, 200, 150);

    QPushButton *quit = new QPushButton("Quit", this);
    quit->setGeometry(50, 40, 75, 30);

    center(this, WIDTH, HEIGHT);

    connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
}

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

    window.setWindowTitle("My window");
    window.setWindowIcon(QIcon("image.jpg"));
    window.show();

    return app.exec();
}

1 个答案:

答案 0 :(得分:1)

在窗口小部件构造函数中使用setWindowFlags(Qt::WindowMinimizeButtonHint|Qt::WindowMaximizeButtonHint|Qt::Window)

了解更多look here