when i tried my code fir the first time
it was
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QMessageBox"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPixmap pix("C:/Users/AMR.EngAmr/Downloads/1561136.jpg");
ui->label_pic->setPixmap(pix);
if(!connOpen())
ui->label_9->setText("فشل الاتصال بقاعدة البيانات");
else
ui->label_9->setText("تم الاتصال بقاعدة البيانات ");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QString Name,period,group,Weight,Notes,Notes2,Notes3,pressure;
Name=ui->lineEdit_name->text();
period=ui->lineEdit_2->text();
group=ui->lineEdit_group->text();
Weight=ui->lineEdit_weight->text();
Notes2=ui->lineEdit_notes2->text();
pressure=ui->lineEdit_presure->text();
Notes=ui->lineEdit_notes->text();
Notes3=ui->lineEdit_notes3->text();
if(!connOpen()){
qDebug()<<"Faield to open the database";
return;
}
connOpen();
QSqlQuery qry;
qry.prepare("insert into Patients (Name,period,group,Weight,Notes2,pressure,Notes,Notes3 ) values('"+Name+"','"+period+"''"+group+"','"+Weight+"','"+Notes2+"','"+pressure+"','"+Notes+"''"+Notes3+"')");
if(qry.exec())
{
QMessageBox::information(this,tr("Save"),tr("Saved"));
connClose();
}
else
{
QMessageBox::information(this,tr("error::"),qry.lastError().text());
}
}
and i got error No query Unable to fetch row
and i tried a solution and my code was
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QMessageBox"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPixmap pix("C:/Users/AMR.EngAmr/Downloads/1561136.jpg");
ui->label_pic->setPixmap(pix);
if(!connOpen())
ui->label_9->setText("فشل الاتصال بقاعدة البيانات");
else
ui->label_9->setText("تم الاتصال بقاعدة البيانات ");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QString Name,period,group,Weight,Notes,Notes2,Notes3,pressure;
Name=ui->lineEdit_name->text();
period=ui->lineEdit_2->text();
group=ui->lineEdit_group->text();
Weight=ui->lineEdit_weight->text();
Notes2=ui->lineEdit_notes2->text();
pressure=ui->lineEdit_presure->text();
Notes=ui->lineEdit_notes->text();
Notes3=ui->lineEdit_notes3->text();
if(!connOpen()){
qDebug()<<"Faield to open the database";
return;
}
connOpen();
QSqlQuery qry;
qry.prepare("insert into Patients (:Name,:period,:group,:Weight,:Notes2,:pressure,:Notes,:Notes3 ) values('"+Name+"','"+period+"''"+group+"','"+Weight+"','"+Notes2+"','"+pressure+"','"+Notes+"''"+Notes3+"')");
qry.bindValue(":Name", Name);
qry.bindValue(":period", period);
qry.bindValue(":group", group);
qry.bindValue(":Weight", Weight);
qry.bindValue(":Notes2", Notes2);
qry.bindValue(":pressure", pressure);
qry.bindValue(":Notes", Notes);
qry.bindValue(":Note3", Notes3);
if(qry.exec())
{
QMessageBox::information(this,tr("Save"),tr("Saved"));
connClose();
}
else
{
QMessageBox::information(this,tr("error::"),qry.lastError().text());
}
}
and i got the error
parameter count mismatch
what is the problem i have to finish it in two days
Looks like you're mixing up your value binding. When you use that, you don't put the values into the query (the very purpose of binding values is avoid that!). This might work, if the column names are correct:
qry.prepare("insert into Patients "
"(Name,period,group,Weight,Notes2,pressure,Notes,Notes3) "
"values(:Name,:period,:group,:Weight,:Notes2,:pressure,:Notes,:Notes3)");
(Note about splitting strings like that, which you probably are not familiar with: in C and C++ writing "foo" "bar" is same as "foobar".)
Also, documentation is your friend. Qt has mostly quite good documentation, and in this case it seems to exactly cover what you are asking.
Related
I'm making a form filling program and I wanted to make a label that when the user picks something from a combobox (name of a person) the address gets filled out in the label. I'm using an sqllite database.
The form looks like this:
Screenshot of how the form is supposed to look like
This is my code for now. The labels name is laAddress
#include "ui_izdavanje_recepta.h"
#include <QDate>
Izdavanje_recepta::Izdavanje_recepta(QWidget *parent) :
QDialog(parent),
ui(new Ui::Izdavanje_recepta)
{
ui->setupUi(this);
modelCBime = new QSqlQueryModel;
modelCBime->setQuery("SELECT idPacijent, ime FROM Pacijent");
ui->imePacCB->setModel(modelCBime);
ui->imePacCB->setModelColumn(1);
modelCBkod = new QSqlQueryModel;
modelCBkod->setQuery("SELECT idDoktor,ime FROM Doktor");
ui->SifraDoktoraCB->setModel(modelCBkod);
ui->SifraDoktoraCB->setModelColumn(0);
QDate cd = QDate::currentDate();
ui->laDatum->setText(cd.toString("d. M. yyyy"));
}
Izdavanje_recepta::~Izdavanje_recepta()
{
delete ui;
}
void Izdavanje_recepta::on_imePacCB_activated(const QString &arg1)
{
}
void Izdavanje_recepta::on_imePacCB_activated(int index)
{
QSqlRecord record=modelCBime->record();
spID = modelCBime->record(index).value("idPacijent").toInt(); //this grabs the id which will later on be put into the database
}`
I am trying to make a Widget that contains multiple widgets with each a QCustomplot and some Plot Data (QLabel and QPushbuttons). This widget is open from a mainwindow with parent=0.
I add the subwidgets with a function, but if it is used more than one time it throws an error:
malloc.c:3757: _int_malloc: Assertion `(unsigned long) (size) >=
(unsigned long) (nb)' failed.
So heres the code, that leads to this:
Opening the widget, that should contain the subwidgets:
Zeitdatenfenster = new Plotfenster();
Zeitdatenfenster->getZeitData(ausgewaehlteKanaele, *messdaten, double(Anfangszeiteingabe->value()), double(Endzeiteingabe->value()));
Zeitdatenfenster->show();
Zeitdatenfenster is in the MainWindow class header : Plotfenster* Zeitdatenfenster;
Constructor:
Plotfenster::Plotfenster(QWidget *parent) :
QWidget(parent),
ui(new Ui::Plotfenster)
{
ui->setupUi(this);
counter=0;
}
getZeitData( ...) -function:
void Plotfenster::getZeitData(const std::vector<bool>& Kanalauswahl, const Messdatenverwaltung& messdaten, double Anfangszeit, double Endzeit){
setWindowTitle("Time Data");
int zahler=0;
for(int i = 0; i<Kanalauswahl.size();i++){
if(Kanalauswahl[i]){
Ploteigenschaften* kanalzeit = new Ploteigenschaften();
std::cout << kanalzeit << std::endl;
geplotteteDatenInhalt.push_back(kanalzeit);
geplotteteDatenInhalt[zahler]->setZeitData(messdaten, i,Anfangszeit,Endzeit);
addPlot(geplotteteDatenInhalt[zahler],zahler);
zahler++;
}
}
}
geplotteteDatenInhalt is a std::vector<Ploteigenschaften*>,
addPlot() is the function where the subwidgets are created. Ploteigenschaften is just a class that stores the data that is plottet and functions to change it.(with the two Qvectors added to the qcustomplot)
AddPlot function:
void Plotfenster::addPlot(Ploteigenschaften* plotDaten, int zahler ){
Plot* aktuellerPlot = new Plot(plotDaten, this);
geplottetes.push_back(aktuellerPlot);
ui->plotcontainer->addWidget(geplottetes[zahler],counter);
counter++;
}
Plot is the subwiget that contains the constuctor:
Plot::Plot(Ploteigenschaften* plotDatens, QWidget *parent) :
QWidget(parent),
ui(new Ui::Plot)
{
ui->setupUi(this);
ui->plotGrid->setColumnStretch(1, 3);
ui->plotGrid->setHorizontalSpacing(20);
derPlot = new QCustomPlot(this);
this->plotDaten = plotDatens;
this->Datensetzten();
}
The programm crashes at the QCustomplot constructor.
In Datensetzten the plot is specified:
void Plot::Datensetzten(){
this->setInfoText();
this->setPlot();
this->setSaveButton();
}
void Plot::setPlot(){
derPlot->addGraph();
derPlot->graph(0)->setData(plotDaten->xDaten,plotDaten->yDaten);
derPlot->xAxis->setRange(plotDaten->xUntereGrenze,plotDaten->xObereGrenze);
derPlot->yAxis->setRange(plotDaten->yMinimum-std::abs(plotDaten->yMinimum)*0.01,plotDaten->yMaximum+std::abs(plotDaten->yMaximum)*0.01);
derPlot->xAxis->setLabel(plotDaten->BeschriftungxAchse);
derPlot->yAxis->setLabel(plotDaten->BeschriftungyAchse);
derPlot->xAxis2->setVisible(true);
derPlot->xAxis2->setTickLabels(false);
derPlot->yAxis2->setVisible(true);
derPlot->yAxis2->setTickLabels(false);
ui->plotGrid->addWidget(derPlot,0,1,2,1);
derPlot->show();
}
Sorry for the long post, but I have no idea where the error is located and would be very glad for som ideas.
Thanks!
I have a class that creates random data which I would like to show in a tableview on the main window.
I added via Designer a table view to the main window and called it tblData.
I suspect the problem is related to this because when I call the constructor the ui file with some implementation is already there.
I have taken the "Detailed Description" section from http://qt-project.org/doc/qt-5/qtablewidget.html as guidance....
However, the table remains empty. I do not see why... Thank you very much.
include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QStringList headers;
headers << "Datapoints";
Dataset *myData;
myData = new Dataset();
myData->createRandomData(10); // create a ten element vector
QVector<int> data;
data = myData->getDataVector(); // data vector created in class Dataset
qDebug() << data;
int i;
for (i = 0 ; i < data.size() ; i++){
QString datapoint;
datapoint = QString::number(data[i]);
QTableWidgetItem * newItem = new QTableWidgetItem(datapoint);
ui->tblData->setItem(i, 0, newItem); // works not
qDebug() << datapoint; // works
}
}
MainWindow::~MainWindow()
{
delete ui;
}
I think you have to define your table's dimensions before starting to populate it with the data, i.e.
ui->tblData->setRowCount(data.size());
ui->tblData->setColumnCount(1);
The reason is that by default the initial row and column count of the table is 0, so the newly added items are not visible.
I'm trying to print an HTML web page that contains Arabic text in Qt. Everything prints fine, but the Arabic text doesn't print properly.
First I tried this:
#include "mainwindow.h"
#include "ui_mainwindow.
#include <QtPrintSupport/QPrinter>
#include <QString>
#include <QtPrintSupport/QPrintDialog>
#include <QTextDocument>
#include <QFile>
#include <QDir>
#include <QFileDialog>
#include <QTextCodec>
#include <QtWebKitWidgets/QWebPage>
#include <QtWebKitWidgets/QWebView>
#include <QtWebKitWidgets/QWebFrame>
QString htmlItem(QString Item)
{
QString r="";
r.append( "<td>"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-family:'Tahoma'; font-size:9pt; font-weight:600;\">");
r.append(Item);
r.append("</span></p></td>");
return r;
}
QString htmlRow(QString MFC,QString MFT,QString D,QString N)
{
QString ret ="" ;
ret.append("<tr>");
ret.append(htmlItem(MFC));
ret.append(htmlItem(MFT));
ret.append(htmlItem(D));
ret.append(htmlItem(N));
ret.append("</tr>");
return ret;
}
QString htmlTable(QString rows)
{
QString ret = "";
ret.append("<table border=\"1\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px;\" align=\"center\" width=\"90%\" cellspacing=\"0\" cellpadding=\"4\">"
"<tbody>");
ret.append(rows);
ret.append("</tbody></table>");
return ret ;
}
QString htmlClientName(QString name)
{
QString retu="<p align=\"center\" style=\" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\""
"font-family:'Tahoma'; font-size:16pt; font-weight:600;\">";
retu.append(name);
retu.append("</span></p>");
return retu;
}
QString htmlClientPage(QString Table,QString Name)
{
QString r = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">"
"<!-- saved from url=(0045)file:///C:/Users/MG/Desktop/Untitled%201.html "
"-->"
"<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><meta name=\"qrichtext\" content=\"1\"><title>QTextEdit Example</title><style "
"type=\"text/css\">"
"p, li { white-space: pre-wrap; }"
"</style></head><body style=\" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-"
"style:normal;\"";
r.append(htmlClientName(Name));
r.append(htmlTable(Table));
r.append("</body></html>");
return r;
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QString cD="";
cD.append(htmlRow("m","n","b","v"));
cD.append(htmlRow("hgfh","n","b","gggg"));
cD.append(htmlRow("m","vvvv","bbbbbbb","v"));
cD.append(htmlRow("m","ssssss","b","sssssssss"));
QString data = htmlClientPage(htmlTable(cD), "???? ???? ");
ui->textEdit->setText(data);
#if !defined(QT_NO_PRINTER) && !defined(QT_NO_PRINTDIALOG)
QPrinter printer(QPrinter::HighResolution);
QPrintDialog *dlg = new QPrintDialog(&printer, this);
if (ui->textEdit->textCursor().hasSelection())
dlg->addEnabledOption(QAbstractPrintDialog::PrintSelection);
dlg->setWindowTitle(tr("Print Document"));
if (dlg->exec() == QDialog::Accepted)
ui->textEdit->print(&printer);
delete dlg;
#endif
}
The output printed file was like this:
I searched for how to solve this problem and ended up with sum bugs in Qt when printing Arabic text. And I tried some other Qwidgets like QwebView and QwebPage and ended up with more problems.
Now I'm asking if there is an alternative way to use Qt printing like Windows API or something else to solve this problem.
I use
SDK : Qt 5.2.1 with Qt Creator 3.0.1
Operating System : Microsoft Windows 8
Edit: I need to mention that Qt prints Arabic text to PDF well.
Edit: I'm pretty sure that the problem is not the encoding. I searched for this problem and I found these bugs in Qt:
https://bugreports.qt-project.org/browse/QTBUG-4452?page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel
I think the problem is related to this one.
This:
QString str = QString::fromLocal8Bit(data.toLocal8Bit());
doesn't make sense to me. I am not sure if it will work, but try the following:
ui->textEdit->setText(htmlArabicPage);
and tell me what happens.
Also if you have this:
htmlClientPage(htmlTable(cD),"محمد جمال ");
in your source code, you should be very careful. If you want to create a Unicode literal in C++11 then you should do:
u8"محمد جمال "
where the text is encoded in UTF-8.
Otherwise, you should try if "محمد جمال " is being interpreted well. Try this:
#include <QString>
#include <QDebug>
int main()
{
qDebug() << QString::fromUtf8("محمد جمال ");
qDebug() << QString::fromLocal8Bit("محمد جمال ");
return 0;
}
The bug you posted is from the previous version of Qt and it has been closed.
So I made a program that simply makes "random" sentences. It chooses a noun and a color adjective from a list of 7 based to a seed that uses ctime. Now I'm trying to convert it into a console app. My issue is that I'm not able to display it correctly. Instead of cout I need to get it all on one label.
error: no matching function for call to
'QLabel::setText(std::string&)'
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <cstdlib>
#include <iostream>
#include <ctime>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_newSentence_clicked()
{
std::string noun[7] = {"cow", "tree", "marker", "cereal", "calendar", "rug", "hammer"};
std::string color[7] = {"red", "orange", "yellow", "green", "blue", "indigo", "violet"};
srand(time(0));
int nounRandomizer = (rand()%5);
int colorRandomizer = ((rand()+1)%5);
std::string sentence = "The"+noun[nounRandomizer]+" is "+color[colorRandomizer]+".";
ui->sentenceDisplay->setText(sentence);
}
From QLabel reference, setText function takes const QString& as input parameter, but you passed in std::string. you could construct a QString object from std::string then pass to it.
For example:
ui->sentenceDisplay->setText(QString::fromStdString(sentence));