[Merged] Inheriting Ui::MainWindow to add QListWidgetItem to a give ListWidget
-
ok so here is the rundown, i simply wish to add a listItem to an exisisting listwidget on the mainform. This item needs to be added from another form called edit. When I actually try to connect the slots with the button clicked signal, I get a segfault. I am assuming it is because i can not access the mainForm widgets but I am not sure why. Here is some code.
Header:
@class MedicationButtonHandler : public QDialog , private Ui::EditMedicationForm , private Ui::MainForm
{Q_OBJECT
public:
virtual ~MedicationButtonHandler();
static MedicationButtonHandler* GetInstance();QListWidgetItem* listItem;
public slots:
QListWidgetItem * MedicationEdit_Add_Clicked();void Medication_Delete_Clicked();
// QString* MedicationNotesClicked();
// void MedicationLearnMoreClicked();
// QString* MedicationFilesClicked();
// void MedicationDiscontinueClicked();
private:
MedicationButtonHandler(QWidget parent = 0);
static MedicationButtonHandler p_MedicationButtonHandler;
static int nextInstance;
Ui::EditMedicationForm *ui;
Ui::MainForm *mainUi;};@
cpp:
@MedicationButtonHandler::MedicationButtonHandler(QWidget *parent) : QDialog(parent), ui(new Ui::EditMedicationForm) , mainUi(new Ui::MainForm)
{ui->setupUi(this);
}
QListWidgetItem* MedicationButtonHandler::MedicationEdit_Add_Clicked()
{MedicationButtonHandler::listItem = new QListWidgetItem(QIcon(":/img/medications.png"), ui->txt_Medication_Name->text()+ " - "+ ui->txt_Medication_Dosage->text()+ ui->cmb_Medication_Dosage->currentText()+ " - " + ui->txt_Medication_Quantity->text()+ ui->cmb_Medication_Quantity->currentText()+" - " + ui->cmb_Medication_Administered->currentText()+ " - " + ui->txt_Medication_Frequency->text()+" - " + ui->txt_Medication_DateStarted->text()+ "-" + ui->txt_Medication_DateModified->text()+ " _ " + ui->txt_Medication_PrescribingDoctor->text()+ " - " + ui->txt_Medication_Pharmacy->text()+ " - " + ui-> txt_Medication_PharmacyPhone->text()+ " - " + ui-> txt_Medication_PrescriptionNum->text() ,lst_Medication_List); listItem->setFlags( Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled); mainUi->lst_Medication_List->insertItem(nextInstance,listItem); return listItem;
}
@
I am not sure as to why this is not working,
-
Well, first off, you should either inherit from your UI class, or include it as a pointer - not both.
Your call to ui->setupUi() is going to crash, because you have never initialized those ui or mainUi pointers.
-
Remove the ui pointers and change the constructor to use
@
Ui::EditMedicationForm::setupUi(this);
Ui::MainForm::setupUi(this);
@or
Remove the Ui::* inheritance and change the constructor to use
@
ui = new Ui::EditMedicationForm;
ui->setupUi(this);
mainUi = new Ui::MainForm;
mainUi->setupUi(this);
@ -
It is not intended that setupUi() of two different Ui classes are called with the main QWidget (this). At best the resulting Ui will look strange. I cannot see any use for this either.
You should have two classes, with .h and .cpp, for either EditMedicationForm and for MainForm.
Your call to mainUi->lst_Medication_List crashes, because you did not call setupUi on mainUi, so the members of mainUi are not yet created.
-
Ok i understand that, but i need to be able to access and modify widgets located in two different UIs, For example, i have a list widget in main form and EditMedicationForm is where the attributes are changed. So I click add new, a new form pops up, I enter the information into the field, and i click save, those attributes need to be added to the listWidget located on main form. now am i wrong or do i not need to have access to both UI's to do this( fyi, I have tried handleing this in the editMedicationForm.cpp as well, i continue to get a seg fault because it will not let me setup the UI as it inherits from a QDialog, not a QMainWindow. Is there a better way to access and modify widgets located in multiple forms? I simply want to add a listwidget item to mainform from data created in editmedicationform. Am i thinking about the incorrectly?
-
[quote author="webmaster.skelton" date="1291900962"]Ok i understand that, but i need to be able to access and modify widgets located in two different UIs, For example, i have a list widget in main form and EditMedicationForm is where the attributes are changed. So I click add new, a new form pops up, I enter the information into the field, and i click save, those attributes need to be added to the listWidget located on main form.[/quote]
Make your ui/mainUi pointers public or declare the respective other class as friends. The members of the uic generated ui classes are public. So you can access them everywhere.
[quote author="webmaster.skelton" date="1291900962"]now am i wrong or do i not need to have access to both UI's to do this( fyi, I have tried handleing this in the editMedicationForm.cpp as well, i continue to get a seg fault because it will not let me setup the UI as it inherits from a QDialog, not a QMainWindow. Is there a better way to access and modify widgets located in multiple forms? I simply want to add a listwidget item to mainform from data created in editmedicationform. Am i thinking about the incorrectly?[/quote]
Be warned: it is common sense, that exposing implementation details to other classes (the ui elements add to that) is considered harmful. It is usually better that you have getters in your classes and forms (your QDialog based class to enter the data) to extract the data you need and provide them in an ui-independend form (Strings, numbers, maybe a small helper class/struct) and to provide a setter (slot) in the other class (MainForm) that does the task of adding the actual list item.
Also, you can not have widgets located in multiple forms! A widget belongs to either exactly one form (as a child widget) or no form at all (that holds for top level widgets like windows, dialogs, etc.)
Sorry, but I think you did not understand the whole concept about user interfaces, forms, widgets and that all. I strongly suggest you do some steps backwards and start with some tutorials and introductions to become comfortable with the basic design of Qt applications.
As a rule of thumb: one ui-file (Designer created form) goes together with exactly one header file (.h) and exactly one C++ implementation file (.cpp) and results in exactly one new custom widget. You can use the widget in other source files by including the header file. Whatever you made public in the class definition is available there.
-
[quote author="Volker" date="1291902228"]As a rule of thumb: one ui-file (Designer created form) goes together with exactly one header file (.h) and exactly one C++ implementation file (.cpp) and results in exactly one new custom widget.[/quote]Agree. Only if you know exactly what you're doing you can somewhat deviate from this rule, however you should probably still step back and rethink your implementation.
-
ok thanks for the help.
-
It should be merged with "this thread":http://developer.qt.nokia.com/forums/viewthread/2139/
-
Yea it can be closed.