I am trying a simple code to load a ui file runtime, but not able to load it.
QUiLoader loader;
QFile file(":/dialog.ui");
file.open(QFile::ReadOnly);
QWidget *myWidget = loader.load(&file, this);
file.close();
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(myWidget);
setLayout(layout);
I have added the dialog.ui file in my .qrc file as well as mentioned here . Not getting any error message. Please tell me what has gone wrong.
Regards
Ok got it.. Here's the code.
QUiLoader uiLoader;
QFile file(":/dialog.ui");
QWidget *sortDialog = uiLoader.load(&file);
if (sortDialog) {
sortDialog->show();
}
Related
I needed to read widget classnames, names etc from gui loaded via QUiloader.
And I got answer here that I needed to subclass QUiloader and reimplement its method.
I have MainWindow, and via menu/toolbar I load the file, and show it. And when I load I want to know what elements are in that gui to work further with them.
The code I got from a user here:
class UiLoader : public QUiLoader
{
Q_OBJECT
public:
QStringList *wlist;
UiLoader(QObject *parent = 0) : QUiLoader(parent) {}
virtual QWidget* createWidget(const QString &className, QWidget *parent =0, const QString &name = QString())
{
QWidget* widget = QUiLoader::createWidget(className, parent, name);
//here do my stuff with className, parent, name
return widget;
}
};
I used Uiloader to load my file. And it is working.
My question now is how I intercept createWidget before returning widget, I want to get the list of widget and then return Widgets.
I do not know how what is the efficient way to create a list as simple as this one:
0=>TextBox, 1=>Button, ...
I really do not care about the structure I just need to know which one comes first and what it is.
Thanks.
I wouldn't reimplement class loader at all honestly.. May be only if you need to 'inject' something into existing code which use QUiLoader a lot.. but in general only thing you need:
pWidget = ...->createWidget(...);
QList<QWidget *> widgets = pWidget->findChildren<QWidget *>();
here you go, your widgets contains list of all form components..
How to build a simple custom widget with Qt? The widget is very simple with just 2 line edit QLineEdit' in a vertical box layoutQVBoxLayout`. How to do it? I read Qt's examples on custom widget generation. They reimplement paint event to rendering the custom widget. However, mine is so simple that I cannot find a solution in Qt's reference.
Well to do it all programatically it would look something like this:
class MyWidget : public QWidget {
public:
MyWidget(QWidget *parent=0) : QWidget(parent) {
QVboxLayout *layout = new QVboxLayout();
setLayout(layout);
layout->addWidget(new QLineEdit());
layout->addWidget(new QLineEdit());
}
};
Depending on your needs you could make the line edits member variables and manipulate them as you see fit.
The main Part of my Application is a Systray-Menu. For maintenance there should be a normal GUI.
My Problem is that now I have to create two Signal/Slot-Connections back to the MainWindow from each Tab. This is for minimizing the GUI and to update the Menu. I don't know how to do this.
I tried to connect with this->parent->parent from the ManageSession and ui_manag->session_ui->minimizeButton from MainWindow. I have a little knot in my head and am asking for help. Or should I re-think my design? I´m using only QtCreator 2.6.1 with Qt 4.8.4.
Screenshots of the GUI-Elements
This is the mainwindows.cpp:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setWindowTitle(QCoreApplication::applicationName());
QWidget *mainWidget = new QWidget;
QTabWidget *ui_manag = new ManageTab;
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(ui_manag);
mainWidget->setLayout(mainLayout);
setCentralWidget(ui_manag);
ui_manag->setCurrentIndex(0);
//Here comming Code to setup a TrayIcon, the Database and the Menus
}
The Tab is completely generated by the Designer:
ManageTab::ManageTab(QWidget *parent) :
QTabWidget(parent),
tab_ui(new Ui::ManageTab)
{
tab_ui->setupUi(this);
}
For each Setting I use the same GUI with multiple inheritance:
ManageSession::ManageSession(QWidget *parent) :
QWidget(parent),
session_ui(new Ui::ManageWidget)
{
session_ui->setupUi(this);
session_ui->manageLabel->setText(tr("Manage Session"));
connect(session_ui->addButton, SIGNAL(clicked()), this, SLOT(addButton_clicked()));
connect(session_ui->editButton, SIGNAL(clicked()), this, SLOT(editButton_clicked()));
connect(session_ui->deleteButton, SIGNAL(clicked()), this, SLOT(deleteButton_clicked()));
}
//Here follows the Functions for manipulating the TableView
// and emmiting a Signal to Update the Menu
Let's remake it in an answer (so you can accept it, hehe. j/k, to long for a comment):
First. As i said in comment:
You are inheriting without specifying access. So it defaults to private. That's why
ui_manag->session_ui->minimizeButton
wont allow you to access the button.
Second.
parent is a method, so It's: this->parent()->parent() or just parent()->parent() ;)
Again, it probably needs to inherit public. Not sure, tho.
That should work then.
I want to load all the images from the text file to widget in Qt.
I have written the code in this way,
void project::showfile()
{
QString fileName = "/home/main/Desktop/image_file.txt";
show_image(fileName);
}
void project :: show_image(const QString &fileName)
{
Widget *wid1=new Widget(ui->scrollArea_2);
QFile file(fileName);
qDebug()<<"test";
if (!file.open(QIODevice::ReadWrite | QIODevice::Text))
{
qDebug() << "Error opening file";
}
QString line;
QTextStream InputDataFile(&file);
while(!InputDataFile.atEnd())
{
line = InputDataFile.readLine();
QPixmap pixmap(line);
wid1->setPixmap(line);
wid1->resize(pixmap.size());
wid1->show();
}
file.close();
}
For your example my image_file.txt contains
/home/keerthana/Desktop/images/2_222_31.jpg
While I am using this code I can load only the last image in the file. I want to load all the images to the GUI.
Can any one help me? That how can I load everything.
The code is not complete, but the code seems to suggest you're loading the image repeatedly on the same widget. It seems correct that only the last is shown. Each pixmap you load seems to replace the previous...
I am trying to print an image file on printer using QWebview but instead of image blank page is printed. Please find the below code.
void ChartViewer::onprintBtnClicked()
{
QString fileName = QFileDialog::getOpenFileName(this,"Open File",QString(),"Pdf File(*.png)");
qDebug()<<"Print file name is "<<fileName;
if(fileName.endsWith(".png"))
{
QPrinter printer;
QWebView *view = new QWebView;
QPrintDialog *dlg = new QPrintDialog(&printer,this);
printer.setOutputFileName(fileName);
if(dlg->exec() != QDialog::Accepted)
return;
view->load(fileName);
view->print(&printer);
}
}
If I use view->show() then it has shown the image properly but printed page is coming blank. Request you to please look into the above code and correct me where I am doing wrong.
Regards,
Lekhraj
You load some png file into fileName. Then you set QPrinter to print to that png file with printer.setOutputFileName(fileName);.I suppose it is wrong, it should be some different pdf file probably.
I'm not sure if I understand what are you trying to do? How to print image file using QPrinter? Into pdf file? Why are trying to use QWebView?
You can use QImage to load image file and then paint with QPainter on QPrinter.
#include <QtGui>
#include <QtCore>
int main(int argc, char** argv) {
QApplication app(argc, argv);
QString fileName = QFileDialog::getOpenFileName(0,"Open File",QString(),"PNG File(*.png)");
QPrinter printer;
QPrintDialog *dlg = new QPrintDialog(&printer,0);
if(dlg->exec() == QDialog::Accepted) {
QImage img(fileName);
QPainter painter(&printer);
painter.drawImage(QPoint(0,0),img);
painter.end();
}
delete dlg;
QTimer::singleShot(1, &app, SLOT(quit()));
app.exec();
return 0;
}
Some of your issues may overlap with your other question
https://stackoverflow.com/questions/8297239/how-to-print-pdf-file-in-qt
You try to print the QWebView immediately after you call its load() function. But the QWebView has not yet loaded the content and the view is therefore blank. You need to connect the QWebView's loadFinished signal to some slot where you can call the print() function. Read the QWebView's documentation.