c++ qt addLayout

#include "mainwindow.h"

#include <QMainWindow>
#include <QPushButton>
#include <QStatusBar>
#include <QTimer>
#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QFormLayout>
#include <QGroupBox>
#include <QFormLayout>

#include <QCoreApplication>
#include <QDebug>
#include <QTextEdit>
#include <QComboBox>

#include <QSpinBox>



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

    QApplication a(argc, argv);
    auto *window = new QWidget;
    // window->setFixedSize(500,500);
    QVBoxLayout *mainLayout = new QVBoxLayout(window);

       auto  layout2 = QHBoxLayout();

    auto *button1 = new QPushButton("Button 1");
    auto *button2 = new QPushButton("Button 2");
    layout2.addWidget(button1);
    layout2.addWidget(button2);

    auto bigEditor = new QTextEdit;
    bigEditor->setPlainText(QObject::tr("bigEditor"));
    bigEditor->setStyleSheet("color: #ff0000;");
    // auto bigEditor2 = new QTextEdit;
    // bigEditor2->setPlainText(QObject::tr("bigEditor2"));
    // bigEditor2->setStyleSheet("color: #ff0000;");
    mainLayout->addWidget(bigEditor,2);

    mainLayout->addLayout(&layout2,2);




    // mainLayout->addSpacing(12);
    // mainLayout->addWidget(bigEditor,2);
    // mainLayout->insertSpacing(0,12);
    // mainLayout->insertStretch(1,4);
    window->show();
    return a.exec();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用C++QT开发的简单聊天系统的示例: ```cpp #include <QtWidgets/QApplication> #include <QtWidgets/QTextEdit> #include <QtWidgets/QLineEdit> #include <QtWidgets/QPushButton> #include <QtWidgets/QVBoxLayout> #include <QtNetwork/QTcpSocket> #include <QtNetwork/QTcpServer> #include <QtNetwork/QHostAddress> #include <QtNetwork/QNetworkInterface> #include <QtCore/QList> class ChatServer : public QTcpServer { Q_OBJECT public: ChatServer(QObject *parent = 0) : QTcpServer(parent) {} protected: void incomingConnection(qintptr socketDescriptor) { ChatSocket *socket = new ChatSocket(this); socket->setSocketDescriptor(socketDescriptor); emit newConnection(socket); } signals: void newConnection(ChatSocket *socket); }; class ChatSocket : public QTcpSocket { Q_OBJECT public: ChatSocket(QObject *parent = 0) : QTcpSocket(parent) { connect(this, SIGNAL(readyRead()), this, SLOT(readData())); connect(this, SIGNAL(disconnected()), this, SLOT(deleteLater())); } public slots: void readData() { while (canReadLine()) { QByteArray line = readLine(); emit messageReceived(line); } } void sendMessage(const QString &message) { write(message.toUtf8()); } signals: void messageReceived(const QString &message); }; class ChatWindow : public QWidget { Q_OBJECT public: ChatWindow(QWidget *parent = 0) : QWidget(parent) { QVBoxLayout *layout = new QVBoxLayout(this); messages = new QTextEdit(this); messages->setReadOnly(true); layout->addWidget(messages); QHBoxLayout *inputLayout = new QHBoxLayout(); input = new QLineEdit(this); inputLayout->addWidget(input); QPushButton *sendButton = new QPushButton(tr("Send"), this); inputLayout->addWidget(sendButton); layout->addLayout(inputLayout); connect(sendButton, SIGNAL(clicked()), this, SLOT(sendMessage())); } void setSocket(ChatSocket *socket) { connect(socket, SIGNAL(messageReceived(QString)), this, SLOT(receiveMessage(QString))); connect(this, SIGNAL(sendMessage(QString)), socket, SLOT(sendMessage(QString))); } public slots: void receiveMessage(const QString &message) { messages->append(message); } void sendMessage() { QString message = input->text(); emit sendMessage(message); input->clear(); } private: QTextEdit *messages; QLineEdit *input; }; int main(int argc, char *argv[]) { QApplication app(argc, argv); ChatServer server; QList<QHostAddress> addresses = QNetworkInterface::allAddresses(); for (int i = 0; i < addresses.size(); i++) { if (addresses[i].protocol() == QAbstractSocket::IPv4Protocol && !addresses[i].isLoopback()) { server.listen(addresses[i], 1234); break; } } ChatWindow window; QObject::connect(&server, SIGNAL(newConnection(ChatSocket*)), &window, SLOT(setSocket(ChatSocket*))); window.show(); return app.exec(); } ``` 这个聊天系统使用QTcpServer和QTcpSocket类来处理客户端连接和消息传递。服务器监听来自局域网下的客户端连接,并将消息转发给所有连接的客户端。客户端可以发送消息并接收其他客户端发送的消息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值