I'm having a little problem once again and maybe you guys could help!
So I'm trying to display database query inside a tableWidget.
Problem is that instead of showing up inside the tableWidget that I've created in the GUI, it opens a completely new program with just that query(without my other buttons or labels).
Screenshot of UI: http://i57.tinypic.com/206eety.png
Instead it looks like this: http://i60.tinypic.com/jkvjuh.png
Question: How do I get that table inside my tableWidget?
Here is my main.cpp:
#include "mainwindow.h"
#include <QApplication>
#include <QtSql/QSql>
#include <QtGui/QtGui>
#include <QMessageBox>
#include <QtSql/QSqlDatabase>
#include <QTableWidget>
#include <QtSql/QSqlError>
#include <QDebug>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlQueryModel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTableWidget* tableWidget = new QTableWidget();
tableWidget->setWindowTitle("Connect to Mysql Database");
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("pizzeria mabo");
db.setUserName("root");
db.setPassword("");
if (!db.open())
{
QSqlError err = db.lastError();
QMessageBox::critical(0, QObject::tr("Database Error"), err.text());
}
QSqlQuery query("SELECT * FROM kunden");
tableWidget->setColumnCount(query.record().count());
tableWidget->setRowCount(query.size());
int index=0;
while (query.next())
{
tableWidget->setItem(index,0,new QTableWidgetItem(query.value(0).toString()));
tableWidget->setItem(index,1,new QTableWidgetItem(query.value(1).toString()));
tableWidget->setItem(index,2,new QTableWidgetItem(query.value(2).toString()));
tableWidget->setItem(index,3,new QTableWidgetItem(query.value(3).toString()));
tableWidget->setItem(index,4,new QTableWidgetItem(query.value(4).toString()));
tableWidget->setItem(index,5,new QTableWidgetItem(query.value(5).toString()));
tableWidget->setItem(index,6,new QTableWidgetItem(query.value(6).toString()));
tableWidget->setItem(index,7,new QTableWidgetItem(query.value(7).toString()));
tableWidget->setItem(index,8,new QTableWidgetItem(query.value(8).toString()));
index++;
}
tableWidget->show();
return a.exec();
MainWindow w;
w.show();
return a.exec();
}
Thank you for the help!
Related
How to show more number(folder) of images in Qlabel or QScrollArea?
QImage image("E:/Raul/Images");
ui.label->setPixmap(QPixmap::fromImage(image));
Like this but i want more number images will load in one label.
Result:
Code:
#include <QApplication>
#include <QLabel>
#include <QLineEdit>
#include <QPointer>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWizardPage>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget widget;
QVBoxLayout *layout=new QVBoxLayout();
QLabel *label=new QLabel("<img src=:/0_0.jpg align=middle><img src=:/0_1.jpg align=middle><strong>Hello</strong> "
"<font color=red>Sai Raul!");
layout->addWidget(label);
widget.setLayout(layout);
widget.show();
return a.exec();
//
}
QLabel is not a web browser, therefore hyperlinks like <img src=/media/cc0-images/grapefruit-slice-332-332.jpg/> doesn't work, but why images from resources can not do it))).
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
QPushButton *trankil = new QPushButton("Clique&&Release", &w);
QVBoxLayout *layoutrkl = new QVBoxLayout;
layoutrkl->addWidget(trankil);
//trankil->move(10,10);
int resultat = 2;
QLCDNumber *lcd = new QLCDNumber(&w);
QLabel *lelabel = new QLabel("bonsoir");
QPushButton *trankil2 = new QPushButton("Clique&&Release2", &w);
layoutrkl->addWidget(trankil2);
layoutrkl->addWidget(lelabel);
layoutrkl->addWidget(lcd);
QObject::connect(trankil, SIGNAL(clicked()),lcd, SLOT(display(resultat)));
w.setLayout(layoutrkl);
w.show();
return a.exec();
}
The connect doesn't work and i don't understand why at all !
There is no problem about how it appear, but if i click the QPushbutton, the QLCD won't display resultat
Thanks for your help
PS : There is my includes :
#include <QApplication>
#include <QVBoxLayout>
#include <QLCDNumber>
#include <QPushButton>
#include <QLabel>
#include <QObject>
If you debug it.
QObject::connect: No such slot QLCDNumber::display(resultat)
display(resultat) is not a slot function.
You can try the following:
QObject::connect(trankil, &QPushButton::clicked, lcd, [&](){
lcd->display(QString::number(resultat));
});
I'd like to show and then close a dialog after 5 seconds. The dialog needs to be automatically resized (horizontally and vertically) based on the content of a label. Here is my code:
#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QTimer>
void notify (int intTime=1000)
{
QDialog notify;
notify.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
notify.setWindowFlag(Qt::FramelessWindowHint);
QLabel *lbl = new QLabel(¬ify);
lbl->setText("This is a test This is a test This is a test This is a test This is a test This is a test This is a test");
QApplication::processEvents();
notify.adjustSize();
QTimer::singleShot(intTime, ¬ify, SLOT(close()));
notify.exec();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
notify(5000);
exit(0);
// return a.exec();
}
It does not not expand the dialog based on the label size. Here is how it looks:
How can I fix it? (Please also let me know if there is better way of doing this.)
I am using Qt5 in Linux.
Since you have not used a QLayout the QLabel will be displayed as large as you can, a possible request is to change the size of QDialog to the recommended size of QLabel with sizeHint():
#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QTimer>
void notify (int intTime=1000)
{
QDialog notify;
notify.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
notify.setWindowFlag(Qt::FramelessWindowHint);
QLabel *lbl = new QLabel(¬ify);
lbl->setText("This is a test This is a test This is a test This is a test This is a test This is a test This is a test");
QApplication::processEvents();
notify.resize(lbl->sizeHint());
QTimer::singleShot(intTime, ¬ify, SLOT(close()));
notify.exec();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
notify(5000);
exit(0);
// return a.exec();
}
The other possible solution is to use a QLayout:
#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QTimer>
#include <QVBoxLayout>
void notify (int intTime=1000)
{
QDialog notify;
QVBoxLayout *lay = new QVBoxLayout(¬ify);
//notify.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
notify.setWindowFlag(Qt::FramelessWindowHint);
QLabel *lbl = new QLabel;
lay->addWidget(lbl);
lbl->setText("This is a test This is a test This is a test This is a test This is a test This is a test This is a test");
QApplication::processEvents();
QTimer::singleShot(intTime, ¬ify, SLOT(close()));
notify.exec();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
notify(5000);
exit(0);
// return a.exec();
}
I am trying to hide my QT application from taskbar? I cannot find anything in Google so I asking here.
Solution from Qt Hide Taskbar Item (Qt Hide Taskbar Item) and this->hide() is not helping.
main.cpp
#include "status_bar.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
status_bar w;
w.show();
return a.exec();
}
status_bar.cpp:
#include "status_bar.h"
#include "ui_status_bar.h"
#include <stdlib.h>
#include <QTime>
#include <QTimer>
#include <QApplication>
#include <QDesktopWidget>
status_bar::status_bar(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::status_bar)
{
ui->setupUi(this);
setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
resize(QApplication::desktop()->width(),36);
ui->time->move(QApplication::desktop()->width()-ui->time->size().width(),10);
ui->username->setText(getenv("USER"));
timeupdate = new QTimer(this);
connect(timeupdate, SIGNAL(timeout()),
this, SLOT(UpdateClock()));
timeupdate->start(100);
}
void status_bar::UpdateClock()
{
ui->time->setText(QTime::currentTime().toString("HH:mm"));
}
status_bar::~status_bar()
{
delete ui;
}
EDIT:
With code like this window is empty.
class MyWindowWidget : public QWidget
{
public:
MyWindowWidget(QWidget *parent)
: QWidget(parent, Qt::Dialog)
{
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
status_bar window;
MyWindowWidget widget(&window);
widget.show();
return app.exec();
}
Solved by using Qt::Tool flag.
Qt::Tool flag has other problems for me, like this widget/windows is hidden when its state becomes inactive.
I would recommend you to use Qt::ToolTip
I'm new to C++ and Qt, and I'm having a bit of trouble getting off the ground. I'd really appreciate some help. I want to add a menubar that will remain constant for all app screens. If I add this to the main function, the menubar shows up fine, but I know this shouldn't be in my main function:
#include <QtGui/QApplication>
#include "mainwindow.h"
#include "form.h"
#include "menu.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
QMenuBar *menu = new QMenuBar(0);
QMenu* menu1 = new QMenu("MENU1");
menu1->addMenu(new QMenu("menu1_SubMenu"));
QMenu* menu2 = new QMenu("MENU2");
menu2->addMenu(new QMenu("menu2_SubMenu"));
menu->addMenu(menu1);
menu->addMenu(menu2);
w.show();
return a.exec();
}
If I create a class for the menu bar, it does not appear. The only difference is that I've put the menu code in the constructor of the menu class and then instantiated the menu class from main.cpp:
main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
#include "form.h"
#include "menu.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
Menu m;
m.show();
w.show();
return a.exec();
}
menu.h
#ifndef MENU_H
#define MENU_H
#include <QMenuBar>
class Menu : public QMenuBar
{
Q_OBJECT
public:
Menu(QMenuBar *parent = 0);
};
#endif // MENU_H
menu.cpp
#include "menu.h"
Menu::Menu(QMenuBar *parent)
{
QMenuBar *menu = new QMenuBar(0);
QMenu* menu1 = new QMenu("MENU1");
menu1->addMenu(new QMenu("menu1_SubMenu"));
QMenu* menu2 = new QMenu("MENU2");
menu2->addMenu(new QMenu("menu2_SubMenu"));
menu->addMenu(menu1);
menu->addMenu(menu2);
}
What am I doing wrong?
Your Menu class is derived from QMenuBar, but you don't call any methods on it except show(). All addMenu() calls are made for the local variable menu in the constructor, which are allocated and then forgotten. You should call them on this instead:
#include "menu.h"
Menu::Menu(QMenuBar *parent)
{
QMenu* menu1 = new QMenu("MENU1");
menu1->addMenu(new QMenu("menu1_SubMenu"));
QMenu* menu2 = new QMenu("MENU2");
menu2->addMenu(new QMenu("menu2_SubMenu"));
this->addMenu(menu1);
this->addMenu(menu2);
}