为什么编译的gui不能以与Qt设计器中预览的大小相同的方式显示?
我的main.cpp:
#include "Send2Server.h"
#include <QtWidgets/QApplication>
#include <QDesktopWidget>
#include <QMainWindow>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QApplication::setStyle("fusion");
QDesktopWidget dw;
Send2Server w;
w.show();
return a.exec();
注释中提供了一些额外的代码:
Send2Server.h:
#include "Send2Server.h"
using namespace std;
Send2Server::Send2Server(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
//insert connect button commands
}
//Commands here
Send2Server.h:
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_Send2Server.h"
#include <QFile>
#include <QFileDialog>
#include <QTextStream>
#include <QMessageBox>
#include <iostream>
#include <string>
using namespace std;
class Send2Server : public QMainWindow
{
Q_OBJECT
public:
Send2Server(QWidget *parent = Q_NULLPTR);
private:
Ui::Send2ServerClass ui;
private slots:
//commands here
};
我的Qt设计风格可能会引起问题:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Send2ServerClass</class>
<widget class="QMainWindow" name="Send2ServerClass">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>434</width>
<height>230</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>Send2Server</string>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<widget class="QWidget" name="centralWidget">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Maximum">
<horstretch>10</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="styleSheet">
<string notr="true">QWidget{
background-color:#282c34;
text-color: white;
}</string>
</property>
<widget class="QLineEdit" name="server_input">
<property name="geometry">
<rect>
<x>110</x>
<y>20</y>
<width>311</width>
<height>21</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">QLineEdit{
background-color: white;
}</string>
</property>
</widget>
<widget class="QPushButton" name="send_file">
<property name="geometry">
<rect>
<x>300</x>
<y>200</y>
<width>56</width>
<height>17</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">QPushButton{
background-color: white;
}</string>
</property>
<property name="text">
<string>Send File</string>
</property>
</widget>
<widget class="QLabel" name="server_address">
<property name="geometry">
<rect>
<x>10</x>
<y>20</y>
<width>81</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<family>Microsoft YaHei UI</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="styleSheet">
<string notr="true">QLabel{
color: white;
}</string>
</property>
<property name="text">
<string>Server Address:</string>
</property>
</widget>
<widget class="QLineEdit" name="user_input">
<property name="geometry">
<rect>
<x>110</x>
<y>60</y>
<width>311</width>
<height>21</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">QLineEdit{
background-color: white;
}</string>
</property>
</widget>
<widget class="QLineEdit" name="pass_input">
<property name="geometry">
<rect>
<x>110</x>
<y>100</y>
<width>311</width>
<height>21</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">QLineEdit{
background-color: white;
}</string>
</property>
</widget>
<widget class="QLabel" name="file_location">
<property name="geometry">
<rect>
<x>40</x>
<y>170</y>
<width>171</width>
<height>16</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">QLabel{
color: white;
}</string>
</property>
<property name="text">
<string/>
</property>
</widget>
<widget class="QPushButton" name="choose_file">
<property name="geometry">
<rect>
<x>220</x>
<y>170</y>
<width>101</width>
<height>16</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="styleSheet">
<string notr="true">QPushButton{
background-color: white;
}</string>
</property>
<property name="text">
<string>Choose File</string>
</property>
</widget>
<widget class="QLabel" name="username">
<property name="geometry">
<rect>
<x>10</x>
<y>60</y>
<width>81</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<family>Microsoft YaHei UI</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="styleSheet">
<string notr="true">QLabel{
color: white;
}</string>
</property>
<property name="text">
<string>Username:</string>
</property>
</widget>
<widget class="QLabel" name="password">
<property name="geometry">
<rect>
<x>10</x>
<y>100</y>
<width>81</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<family>Microsoft YaHei UI</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="styleSheet">
<string notr="true">QLabel{
color: white;
}</string>
</property>
<property name="text">
<string>Password:</string>
</property>
</widget>
<widget class="QLabel" name="private_key">
<property name="geometry">
<rect>
<x>10</x>
<y>140</y>
<width>81</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<family>Microsoft YaHei UI</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="styleSheet">
<string notr="true">QLabel{
color: white;
}</string>
</property>
<property name="text">
<string>Private Key:</string>
</property>
</widget>
<widget class="QLabel" name="file">
<property name="geometry">
<rect>
<x>10</x>
<y>170</y>
<width>81</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<family>Microsoft YaHei UI</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="styleSheet">
<string notr="true">QLabel{
color: white;
}</string>
</property>
<property name="text">
<string>File:</string>
</property>
</widget>
<widget class="QPushButton" name="choose_key">
<property name="geometry">
<rect>
<x>220</x>
<y>140</y>
<width>101</width>
<height>16</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">QPushButton{
background-color: white;
}</string>
</property>
<property name="text">
<string>Choose your Private Key</string>
</property>
</widget>
<widget class="QLabel" name="key_location">
<property name="geometry">
<rect>
<x>90</x>
<y>140</y>
<width>131</width>
<height>16</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">QLabel{
color: white;
}</string>
</property>
<property name="text">
<string/>
</property>
</widget>
<widget class="QPushButton" name="clear_file">
<property name="geometry">
<rect>
<x>340</x>
<y>170</y>
<width>81</width>
<height>16</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">QPushButton{
background-color: white;
}</string>
</property>
<property name="text">
<string>Clear File</string>
</property>
</widget>
<widget class="QPushButton" name="clear_key">
<property name="geometry">
<rect>
<x>340</x>
<y>140</y>
<width>81</width>
<height>16</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">QPushButton{
background-color: white;
}</string>
</property>
<property name="text">
<string>Clear Private Key</string>
</property>
</widget>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources>
<include location="Send2Server.qrc"/>
</resources>
<connections/>
</ui>