对于QT,到目前为止,我还是非常不happy的。
学习QT之前,看到QT应用那么广,然后看到QT做出的图形效果那么好,当时是对QT抱有很大的期望的,但是今天一学,顿时就萎了。
入门老师花一个小时把QT能讲的东西都讲了,汗颜~
今天花了一上午去摸索,主要是摸索如何在画好界面之后,然后将界面和程序连接起来,结果搞了半天就把自己搞得超级超级烦。主要是自己把自己绕在那个界面上的信号和槽,老想通过f4来连。结果到最后在控件上一个“转到槽”+double信号函数就回到了熟悉的VC了,哎,真纠结。
之后,就自己写了个登录窗口。一上午就这么Over了,就为了这个窗口,一上午,就应该留作纪念的。
//QtLogin.pro
#-------------------------------------------------
#
# Project created by QtCreator 2012-07-24T09:36:42
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = QtLogin
TEMPLATE = app
SOURCES += main.cpp\
widget.cpp
HEADERS += widget.h
FORMS += widget.ui
//widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void on_loginPushButton_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
//widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>
#include <QDesktopWidget>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ui->pwdTextEdit->setEchoMode(QLineEdit::Password);
setAutoFillBackground(true);
QPalette palette;
palette.setBrush(QPalette::Background, QBrush(QPixmap("C:/pic/dear.jpg")));
setPalette(palette);//shezhi
this->setFixedSize(this->width(),this->height());
QDesktopWidget* desktop = QApplication::desktop();
move(desktop->width()/3,desktop->height()/4);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_loginPushButton_clicked()
{
if( ui->usrTextEdit->text()==tr("qt") && ui->pwdTextEdit->text()==tr("qwe123") || ui->usrTextEdit->text()==ui->pwdTextEdit->text()){
QMessageBox::information(this, tr("Login success !"), tr("Welcome to ** System !"), QMessageBox::Ok);
}
else{
QMessageBox::warning(this, tr("Login failed"), tr("Error user name or password, please retry.\r\nLogin failed!"),QMessageBox::Yes);
//QMessageBox::information(NULL, "Title", "Content", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
ui->pwdTextEdit->clear();
ui->usrTextEdit->clear();
}
}
//main.cpp
#include <QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
//widget.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>505</width>
<height>383</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>60</x>
<y>100</y>
<width>54</width>
<height>20</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string>用户名:</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>60</x>
<y>180</y>
<width>54</width>
<height>20</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string>密 码:</string>
</property>
</widget>
<widget class="QPushButton" name="loginPushButton">
<property name="geometry">
<rect>
<x>60</x>
<y>280</y>
<width>131</width>
<height>51</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string>登录</string>
</property>
</widget>
<widget class="QPushButton" name="exitPushButton">
<property name="geometry">
<rect>
<x>290</x>
<y>282</y>
<width>121</width>
<height>51</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string>退出</string>
</property>
</widget>
<widget class="QLineEdit" name="pwdTextEdit">
<property name="geometry">
<rect>
<x>140</x>
<y>170</y>
<width>271</width>
<height>41</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>20</pointsize>
</font>
</property>
</widget>
<widget class="QLineEdit" name="usrTextEdit">
<property name="geometry">
<rect>
<x>140</x>
<y>90</y>
<width>271</width>
<height>41</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>20</pointsize>
</font>
</property>
</widget>
<widget class="QLabel" name="label_3">
<property name="geometry">
<rect>
<x>80</x>
<y>20</y>
<width>351</width>
<height>41</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>36</pointsize>
</font>
</property>
<property name="text">
<string>**欢迎你</string>
</property>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections>
<connection>
<sender>exitPushButton</sender>
<signal>clicked()</signal>
<receiver>Widget</receiver>
<slot>close()</slot>
<hints>
<hint type="sourcelabel">
<x>222</x>
<y>143</y>
</hint>
<hint type="destinationlabel">
<x>268</x>
<y>137</y>
</hint>
</hints>
</connection>
</connections>
</ui>