I have such a code, according to my logic, it must assign what is entered in QLineEdit - serialNumberText , the variable serialnumberPDF .

But I get the error:

"C ++ cannot convert QLineEdit to char"

Code:

 QLineEdit *serialNumberText; char serialnumberPDF[3]; void Main::finish(){ strcpy(serialNumberText, serialnumberPDF); // ОСТАЛЬНОЙ КОД // } 

How it works, I have QLineEdit , where I enter for example 4005, then I click on the " Finish " button, now the program should from serialNumberText save the number to the variable serialnumberPDF , and then save it to a PDF file - .arg(serialnumberPDF)

I add the code on request:

 char serialnumberPDF[3]; void Main::finish(){ if (sdRemoveCheck->isChecked()){ QByteArray text = serialNumberText->text().toLocal8Bit(); text.resize(sizeof(serialnumberPDF) -1); strcpy(serialnumberPDF, text.data()); pdf_creation2(); close(); }else{ QMessageBox messageBox; messageBox.critical(0, "Error", "Remove first mircoSD Card"); messageBox.setFixedSize(500, 200); } QString SerialNumber = QString("<tr class=\"method\">" "<th style=\"background: #e3e4e4;\">S/N</th>" "<td>%1</td>" "<td></td>" "</tr>").arg(serialnumberPDF); 
  • one
    Tell us better what you are going to do with serialNumberText . - αλεχολυτ
  • And with serialNumberPDF too. - aleks.andr
  • updated question, check - Insider

2 answers 2

If you want to fill an array with what is contained in a QLineEdit , do this:

 #include<QByteArray> #include<QString> QLineEdit *serialNumberText; char serialnumberPDF[3]; void Main::finish(){ QByteArray text = serialNumberText->text().toLocal8Bit(); text.resize(sizeof(serialnumberPDF) - 1); strcpy(serialnumberPDF, text.data()); } 

Ps : array char? Seriously? You do not write to C. Use QString , well, or if you have something there that accepts the char array, then use QByteArray . And do not need these squats with strcpy

  • unfortunately I'm still used to int and char ... I need to somehow retrain. - Insider
  • one
    @Insider, Start right now. A huge bunch of errors in the code falls on all sorts of memcpy , memset , strcpy , etc. And this is for experienced developers in large projects - yrHeTaTeJlb
  • unfortunately the same way, the critical close - Insider
  • @Insider, a problem in another part of the code. Run under the debugger and see where it crashes - yrHeTaTeJlb
  • falls in this part of the code, the debugger itself shows. The rest checked everything. - Insider

All right, the compiler is writing to you. He tried unsuccessfully to convert a pointer to a QLineEdit to a pointer to a char .

Suppose that a user is required to enter a number in the field:

 // где-то ранее описаны QLineEdit* m_lineEdit и QPushButton* m_button // затем вызван connect(m_button, SIGNAL(clicked()), SLOT(slotOnClick())); void slotOnClick() { QString str = m_lineEdit->text(); if (str.isEmpty()) { // скажите пользователю, что он ничего не ввёл return; } bool ok = false; int serialNumber = str.toInt(&ok); if (!ok) { // скажите пользователю, что он должен был ввести число, а не абракадабру return; } // требуемый вам serialNumber готов к использованию, // теперь можно делать так: QString result = QString("Вы ввели число: %1").arg(serialNumber); qDebug() << result; } 
  • unfortunately now I get the following error class QString has no member named toAscii - Insider
  • Use QByteArray QString::toLatin1() const . - aleks.andr
  • serialnumberPDF is not a class or namespace ... I updated the question a bit, can it help now? - Insider
  • in the end, after pressing the button, the program closes .. the button works correctly - Insider
  • Most likely your program does not close, but it crashes. I have no doubt that the button works correctly - your slot does not work correctly. Most likely you are working incorrectly somewhere with memory. - aleks.andr