int main(int argc, char *argv[])
{
QApplication a(argc, argv);
After making connection, I tried this code , but it doesn't display any data
QSqlTableModel *model=new QSqlTableModel;
model->setTable("tamp");
model->select();
QTableView *view = new QTableView;
view->setModel(model);
view->show();
db.close();
return a.exec()
}
Related
i want to remove the icon on the top left of a qt application. here is my main.cpp code
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
Receive receive;
QObject *topLevel = engine.rootObjects().value(0);
QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
// setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint);
QObject::connect(window,SIGNAL(sendUrltoC(QUrl)),&receive,SLOT(getText(QUrl)));
return app.exec();
}
if i uncomment the commented part it gives me an error. what should i do?
Thank you for going through this post. I have searched forums,blogs and SO, but could not get what I actually need.
I am experimenting on how to display multiple mainwindows. I am using a embedded hardware board. I have successfully ported QT lib on to it.
I have written a small program.
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
main.cpp
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow text_plane;
text_plane.setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
text_plane.setStyleSheet("background-color: Black;");
text_plane.show();
a.exec();
return a.exec();
}
The above code displays one window only.
Even if I create a Mainwindow w1 after text_plane.show() like
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow text_plane,w1;
text_plane.setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
text_plane.setStyleSheet("background-color: Black;");
text_plane.show();
w1.setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
w1.setStyleSheet("background-color: Yellow;");
w1.show();
a.exec();
return a.exec();
}
Now only the w1 window is shown. What about the text_plane window ?? how to get that back.
Can anyone help me out here to make this understand.
Thank you
First of all, I wonder, what are you trying to achieve here:
a.exec();
return a.exec();
You need to call this method only once, you know.
Second of all, both of your windows are shown (you can see it in a taskbar), but, since you set Qt::FramelessWindowHint, one of them is drawn on top of the other. You can split them by using move (or something like it):
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow w, w1;
w.setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
w.setStyleSheet("background-color: Black;");
w.show();
w1.setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
w1.setStyleSheet("background-color: Yellow;");
w1.show();
w.move(0, 0);
w1.move(100, 100);
return app.exec();
}
I'd like to use the class QFileSystemModel to show my file system in QTreeView.
But when I set thr root path in it.I try to get its first filename.It failed.I want to ask that how should I do to get parent's child info. Thanks
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTreeView view;
QFileSystemModel *model = new QFileSystemModel(&view);
model->setRootPath(QDir::currentPath());
view.setModel(model);
view.setCurrentIndex(model->index(model->rootPath()));
QModelIndex index = model->index(model->rootPath());
qDebug() << model->fileName(index);
qDebug() << model->fileName(index.child(0,0));
view.show();
return a.exec();
}
Try with:
QFileSystemModel *model = new QFileSystemModel;
model->setRootPath(QDir::currentPath());
QTreeView *tree = new QTreeView(splitter);
tree->setModel(model);
splitteris parent Dialog, which view will reside in.
I have set ObjectName to a class in main and want to access this object from another class:
main.cpp
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
ImageProvider *imageProvider = new ImageProvider(&engine,QQmlImageProviderBase::Image,0);
PageBuffer p;
p.setObjectName("Object");
engine.rootContext()->setContextProperty("p",&p);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
engine.addImageProvider("images", imageProvider);
return app.exec();
}
The class where i am trying to access PageBuffer p from:
QImage ImageProvider ::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
QQuickWindow *window = qobject_cast<QQuickWindow*>(m_engine->rootObjects()[0]);
PageBuffer *p = window->findChild<PageBuffer *>("Object");
cout<<p->current_box; //error at runtime
QImage e;
e.load("Pic4.jpg");
return e;
}
But i cant seem to access any member of the object PageBuffer, maybe because of pointers, how can can i access the functions from the object p?
You could just introduce a pageBuffer member to ImageProvider:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
ImageProvider *imageProvider = new ImageProvider(&engine,QQmlImageProviderBase::Image,0);
PageBuffer p;
imageProvider->setPageBuffer(p);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
engine.addImageProvider("images", imageProvider);
return app.exec();
}
Then you don't need to search for object children, you can access pageBuffer directly:
QImage ImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
QQuickWindow *window = qobject_cast<QQuickWindow*>(m_engine->rootObjects()[0]);
PageBuffer *p = pageBuffer();
cout << p->current_box;
QImage e;
e.load("Pic4.jpg");
return e;
}
I'm trying to show a start-up image using QSplashScreen and I want to show the image for about 2 seconds.
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
QPixmap pixmap(":/images/usm.png");
QSplashScreen splash(pixmap);
splash.show();
splash.showMessage("Loading Processes");
QTimer::singleShot(2000, &splash, SLOT(close()));
MainWindow w;
w.show();
splash.finish(&w);
return a.exec();
}
But this is not working. QSplashScreen appears for some milliseconds and then disappears. Tried to modify the time period but it seems like the QSplashScreen object is not connected to slot. What's the problem and how to avoid it?
The problem with your code is that the timer is not blocking the execution so the splash screen has already closed with the splash.finish(&w) call. What you need is a sleep. You could use a QWaitCondition like this:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSplashScreen splash(QPixmap(":/images/usm.png"));
splash.show();
splash.showMessage("Loading Processes");
// Wait for 2 seconds
QMutex dummyMutex;
dummyMutex.lock();
QWaitCondition waitCondition;
waitCondition.wait(&dummyMutex, 2000);
MainWindow w;
w.show();
splash.finish(&w);
return a.exec();
}
The disadvantage of this approach is that you are blocking the execution. If you do not want to block it then you can simply remove the splash.finish(&w) call:
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
QPixmap pixmap(":/images/usm.png");
QSplashScreen splash(pixmap);
splash.show();
splash.showMessage("Loading Processes");
QTimer::singleShot(2000, &splash, SLOT(close()));
MainWindow w;
w.show();
return a.exec();
}
This code should work:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSplashScreen splash(QPixmap(":/images/usm.png"));
splash.showMessage("Loading Processes");
splash->show();
QMainWindow w;
QTimer::singleShot(2000, splash, SLOT(close()));
QTimer::singleShot(2500, &w, SLOT(show()));
return a.exec();
}