9_25_messageBox

messageBox

设置messageBox信息

这种设置方式一般在我们需要仔细设置messageBox 的信息,

void MainWindow::on_pushButton_clicked()
{
    //创建一个对话框
    QMessageBox* messageBox = new QMessageBox();
    messageBox->setWindowTitle("这是一个对话框标题");
    messageBox->setText("这是一个对话框文本");
    messageBox->setIcon(QMessageBox::Information);
    messageBox->setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel | QMessageBox::Save);
    //除了上述设置按钮的方法,还有可以自定义设置按钮的方法
    QPushButton* button = new QPushButton("Ok");
    messageBox->addButton(button,QMessageBox::AcceptRole);


    //模态的到这里就会阻塞,所以直接delete和调用函数都可以
    int result = messageBox->exec();

    //直接delete
    //delete messageBox;
    //调用函数
    messageBox->setAttribute(Qt::WA_DeleteOnClose);

    //对对话框的返回值进行反馈
    if(result == QMessageBox::Ok)
    {
        qDebug() << "Ok";
    }
    else if(result == QMessageBox::Save)
    {
        qDebug() << "Save";
    }
    else if(result == QMessageBox::Cancel)
    {
        qDebug() << "Cancel";
    }
}

通过函数设置messageBox信息,QMessageBox提供的静态函数,只能显示出简单对话框,需要详细设计请看上面

void MainWindow::on_pushButton_clicked()
{
    //静态函数创建message
    int result = QMessageBox::warning(this, "对话框标题", "对话框文本", QMessageBox::Ok|QMessageBox::Cancel);
    if(result == QMessageBox::Ok)
    {
        qDebug()<<"Ok";
    }
    else if(result == QMessageBox::Cancel)
    {
        qDebug()<<"Cancel";
    }
}

下位机代码如下: ```c #include <SPI.h> #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define OLED_RESET 4 Adafruit_SSD1306 display(OLED_RESET); #define CS_PIN 10 // W25QXX模块的片选引脚 void setup() { Serial.begin(9600); SPI.begin(); display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // 初始化OLED屏幕 display.clearDisplay(); display.display(); } void loop() { if (Serial.available()) { char data = Serial.read(); if (data == 'w') { // 如果收到w,则进行写操作 byte buffer[4]; for (int i = 0; i < 4; i++) { while (!Serial.available()); // 等待数据传输完成 buffer[i] = Serial.read(); } digitalWrite(CS_PIN, LOW); // 选中W25QXX模块 SPI.transfer(0x02); // 发送写指令 SPI.transfer(0x00); // 发送地址的高8位(因为FLASH容量不超过16MB,所以只需要发送3个字节的地址) SPI.transfer(0x00); // 发送地址的中间8位 SPI.transfer(0x00); // 发送地址的低8位 for (int i = 0; i < 4; i++) { SPI.transfer(buffer[i]); // 发送数据 } digitalWrite(CS_PIN, HIGH); // 取消选中W25QXX模块 } else if (data == 'r') { // 如果收到r,则进行读操作 byte buffer[4]; digitalWrite(CS_PIN, LOW); // 选中W25QXX模块 SPI.transfer(0x03); // 发送读指令 SPI.transfer(0x00); // 发送地址的高8位 SPI.transfer(0x00); // 发送地址的中间8位 SPI.transfer(0x00); // 发送地址的低8位 for (int i = 0; i < 4; i++) { buffer[i] = SPI.transfer(0x00); // 读取数据 } digitalWrite(CS_PIN, HIGH); // 取消选中W25QXX模块 for (int i = 0; i < 4; i++) { Serial.write(buffer[i]); // 将读取到的数据发送给上位机 } display.clearDisplay(); display.setCursor(0, 0); display.print("Data:"); for (int i = 0; i < 4; i++) { display.setCursor(0, 10 * (i + 1)); display.print(buffer[i], HEX); // 将读取到的数据显示在OLED屏幕上 } display.display(); } } } ``` 上位机代码如下: ```csharp using System; using System.IO.Ports; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; namespace FlashReadWrite { public partial class Form1 : Form { private SerialPort port; public Form1() { InitializeComponent(); port = new SerialPort("COM3", 9600); // 指定串口号和波特率 port.Open(); } private void btnWrite_Click(object sender, EventArgs e) { if (tbxData.TextLength == 4) { port.Write("w"); // 发送写指令 for (int i = 0; i < 4; i++) { port.Write(tbxData.Text[i].ToString()); // 发送数据 } } else { MessageBox.Show("请输入4个字符!"); } } private void btnRead_Click(object sender, EventArgs e) { port.Write("r"); // 发送读指令 for (int i = 0; i < 4; i++) { tbxData.Text += ((char)port.ReadByte()).ToString(); // 读取数据并显示在文本框中 } } private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) { if (port.BytesToRead >= 4) { byte[] buffer = new byte[4]; port.Read(buffer, 0, 4); // 读取下位机发送的数据 this.Invoke(new Action(() => { chart.Series[0].Points.Clear(); for (int i = 0; i < 4; i++) { chart.Series[0].Points.Add(buffer[i]); // 将数据显示在图表中 } })); } } } } ``` 注意:上位机代码需要使用Chart控件,需要在Visual Studio的工具箱中添加Chart控件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值