---
title: "qt小结"
author: "openstudy"
date: "2011-01-10T18:27:50Z"
tags: []
categories: ["编程语言"]
slug: "qt"
draft: false
lastmod: "2011-01-10T18:44:38Z"
id: 867514
url: "https://openstudy.iteye.com/blog/867514"
---

1）#include <QHostInfo> 报错fatal error: QHostInfo：No such file or directory，解决办法
QT        += core gui network 记得这里要加上network 

2）窗口居中显示
QDesktopWidget* desktop = QApplication::desktop();
move((desktop->width() - this->width())/2,
	 (desktop->height() - this->height())/2);

3）控件在布局中居中显示
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addWidget(loginButton, 0, Qt::AlignCenter);

4）窗口最大化显示
setWindowState(Qt::WindowMaximized);

5）请注意，调用一个QPushButton对象的setDefault(true)方法仅当这个按钮用于一个基于对话框的主窗口且没有默认按钮时才有效。如果你试图为一个主窗口定义默认按钮，Qt将使得它看起来像一个默认按钮，但是当用户按回车键时无法激活它
另外有人给我回复：The default button behavior is provided only in dialogs.

6）响应回车事件
void LoginWindow::keyPressEvent(QKeyEvent* event) {
	if (event->key() == Qt::Key_Return) {
		sendMessage();
	}
}