Refresh QTableWidget with new values - qt

I have two forms - form1, form2.
form1 has got a QTableWidget which reads and shows xml entries in folder.
form2 is a dialog form, collect user data and save as xml.
I need to implement the following:
Pressing button1 in form2 save (this is working) and close the window and immediately refresh QTabletWidget in form1 with new values.
Could you show me a way to implement this?

An easy workaround without signal & slot:
close form 1(btw , it is QMainwindow) when form 2 is created. Then when I press button form 2 will be closed and a new form 1 will be created. Like this:
this->close(); ---->form 2
MainWindow *back = new MainWindow(this);
back->show();

Related

Qt pass data between multiple forms

I am new to Qt and understands the concept of signal and slots. However I am not able to implement it.
My objective is:
Form1 has a button Config. So when I click Config it should open another form Form2( without closing Form1) and send a string strData to Form2.
In Form2 I set some value in the string strData. Then I click Ok button in Form2, Form2 should close and return back the string to Form1.
When the call returns back to Form1, it should continue from where it emitted the signal to invoke Form2.
Any help is highly appreciated.
You can't do this using signals/slots; the signal is emitted, and all of the connected slots are executed, and then the code continues from where the signal is emitted and eventually returns to the event loop. That's when your second form is actually shown and the user can respond to it, but by then, your code is long past where the signal was emitted.
What I believe you're looking for is the QDialog::exec method; use it in place of the signal. The basic pattern of the code is:
// This is the response to click on Config...
Form2Dialog form2;
form2.setSomeStringValue (some_value);
if (form2.exec() == QDialog::Accepted)
{
QString some_new_value = form2.newValue();
}
The Form2Dialog is a subclass of QDialog where you've added your own setSomeStringValue and newValue methods. (What you actually name these is up to you.)
The important thing is that the exec method blocks and doesn't return until the user selects OK or Cancel on the dialog, or closes it using the "close" button in the title bar (if there is one).

CodeName One about lists

I have a problem I want to use the list and when I click on a component of the list it take me to another form (make an action) meaning if I click on an item from the list depending on the selected index it takes me to another form.
Is this in a GUI builder app or manual app?
For GUI builder use the action event callback for the list see: http://www.codenameone.com/how-do-i---handle-eventsnavigation-in-the-gui-builder--populate-the-form-from-code.html
Then get the index/selected item using:
int index = list.getSelectedIndex();
Object value = list.getSelectedItem();
Then you can show the form you want using:
showForm("FormName", null);
In handcoded apps you can get the selected item/index in exactly the same way then do something like:
form.show();
To show a specific form.

using lambdas or QSignalMapping to pass a custom class value into custom SLOT

I am using Qt5.
I have a loop which generates multiple (number specified by the user) plots by using QCustomPlot (http://www.qcustomplot.com/) each shown in their own dialog. I want the user to be able to save one of the plots, so in each dialog there is a menu bar with an Action "Save as PDF".
I have a List of the plots (QList< QCustomPlot*> >) which each plot is added to when it is created in the loop. My issue is how to select from the list which plot should be saved when the user triggers the action. Here's the main code:
while(currentPlotNum<NumPlots){
//code for generating plots
QAction *saveAsPdfAction = new QAction("Save As PDF",plotDialog);
QFileDialog *saveAsPdfDialog = new QFileDialog(plotDialog);
saveAsPdfDialog->setFileMode(QFileDialog::AnyFile);
saveAsPdfDialog->setNameFilter("PDF Files (*.pdf)");
QObject::connect(saveAsPdfAction,SIGNAL(triggered()),saveAsPdfDialog,SLOT(exec()));
QSignalMapper *signalMapper = new QSignalMapper(saveAsPdfDialog);
QObject::connect(saveAsPdfAction,SIGNAL(triggered()),signalMapper,SLOT(map()));
signalMapper->setMapping(saveAsPdfAction,currentPlotNum);
QObject::connect(signalMapper,SIGNAL(mapped(int)),this,SLOT(setWorkingPlot(int)));
QObject::connect(saveAsPdfDialog,SIGNAL(fileSelected(QString)),this,SLOT(saveToPDF(QString)));
currentPlotNum++;
}
then here are the two SLOTS:
void samplePlots::setWorkingPlot(int value){
workingPlot = value;
}
void samplePlots::saveToPDF(QString PdfFileName){
plotList[workingPlot]->savePdf(PdfFileName,false,600,600);
}
I run the application and generate say 3 plots, when I click the button to save one of the plots, the plot that actually gets saved is seemingly a random choice of one of the 3, rather than the plot in the dialog which i click the button in.
Ideally I would have been able to pass the QCustomPlot* itself through the SignalMaper, but it doesn't seem as though I can do that. I also tried to have the Slot as a lambda (following the syntax here http://www.artandlogic.com/blog/2013/09/qt-5-and-c11-lambdas-are-your-friend/ but I couldn't get it to work.
If anyone has Ideas of how to fix my problem that would be great.
Connect each 'saveToPdf buttons' triggered(bool) signal to a custom signal of your derived displaying QDialog (lets call it saveRequested()).
store in the dialog the index of the displaying plot as well and save your QSignalMapper (not needed).
then connect your main class where your list is stored to that saveRequested() signal, cast the QObject::sender() to your Dialog and access the plot in the list.
cheers

Qtreewidget -- undo the selection change

In wx-widget we can undo any event by calling VETO().
Here i am doing my first GUI in QT. I have created a new node test case 3.
Now i want that selection on tree should be only allowed to changed till i have not saved this new node. If i have not saved this node at least once, selection change should revert back to this unsaved node test case 3. To indicate that i have saved the node i am using a global variable signal.
How can i achive it?
I tried something like this but no result.
Inside selection changed event handler when new node created signal is set, then if i change the treewidget selection i am trying to set the selection of the treewidget to the last item of the treewidget:
QPoint prevPoint;
QModelIndex index;
int count = ui->treeWidgetLeft->topLevelItemCount();
//prevPoint.setX(currentXmlRootNodeNumber +1 );
prevPoint.setX(count);
prevPoint.setY(0);
index = ui->treeWidgetLeft->indexAt(prevPoint);
abortEvent = TRUE;
ui->treeWidgetLeft->selectionModel()->select(index ,QItemSelectionModel::Select);
And check at begning of selection changed event handler:
if (abortEvent) {
abortEvent = false;
return;
}
That's not how the users expect treewidget and selections to work. I suggest that you reconsider the workflow of your program.
What if you let the user change the test case without saving when the user selects another test case from the treewidget? Show a messagebox and ask if the user wants to save. Then save/discard and change the test case to the one the user clicked.
Or remove the "Save" button completely. Always save the data the user enters to the test case. If you like, you could add an undo button instead. It would restore the test case to the state it was when the user selected it. That is quite easy to implement, just create a copy of the test case when editing starts and revert to that if the user selects the undo operation.
And I really recommend that you change the "Perv" button to "Prev".

preesing enter on QLineEdit terminates the screen

i have a QLineEdit on my main screen define by QDialog.along with it i have a table which contains dynamic data displayed by QThread with 50 data in every 2 seconds.when i input any value in QLinrEdit and then press enter then the screen terminates.
value = new QLineEdit(this);
m_label = new QLabel(tr("&Enter Preference Value:"));
m_label->setBuddy(value);
m_preLayout->addWidget(m_label);
m_preLayout->addWidget(value);
m_preferenceGroup->setLayout(m_preLayout);
connect(value, SIGNAL(returnPressed()), this, SLOT(preferentialData()));
void appWindow::preferentialData()
{
valuee = (value->text()).toInt();
}
here i am taking the input from user and then converting that input into an integer which will be further used for some other purpose.Now after taking that input as per signal i press enter and as soon as after that the screen closes.
the value is converted to int and no errors are coming on compiling but why is the window closing? because if it closes then the thing that i will further do with that converted int will be like of no use as with the help of that int i will change some display on my table as i mentioned that i have a table too in that window.
thanks for any help in advance
I'm almost certain (can't be sure without seeing more code) that the dialog is taking the "Enter" keypress and calling its accept() method, which closes the dialog. If you made the dialog in Qt Creator and chose one of the dialog types that places a button box on the form for you, then this connection is wired up by default.
Check your dialog's signal/slot connections and make sure that the accept() slot isn't connected to a QPushButton or QDialogButtonBox signal.

Resources