QT 在Main文件中调用子窗口控件

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/cleverhorse/article/details/85704682

QT 在Main文件中调用子窗口控件

所遇问题

有时候在qt界面切换的时候,需要用到Main对界面进行宏观调控,那么就需要在Main文件中调用子窗口的控件。

方法

  1. #include "ui_widget.h"这个文件引入到main.c文件中
  2. 将视图中的ui变量变成公有变量
    public:
        Ui::Widget *ui;
  1. 声明一个UI类指针并指向所需改变视图的ui变量
    Ui_Widget *PUi_Widget;
    PUi_Widget=w.ui;
  1. 此时在Main文件里就可以像普通使用一样的使用内部函数了
    PUi_Widget->label->setText("123");

代码展示

Main.cpp

#include "widget.h"
#include <QApplication>
#include "ui_widget.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w(0,0);
    w.show();
    Ui_Widget *PUi_Widget;
    PUi_Widget=w.ui;
    PUi_Widget->label->setText("123");
    return a.exec();
}

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent,int ID);
    ~Widget();

public:
    Ui::Widget *ui;
};

#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include <QApplication>
#include "ui_widget.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w(0,0);
    w.show();
    Ui_Widget *PUi_Widget;
    PUi_Widget=w.ui;
    PUi_Widget->label->setText("123");
    return a.exec();
}

widget.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Widget</class>
 <widget class="QWidget" name="Widget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Widget</string>
  </property>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>60</x>
     <y>60</y>
     <width>72</width>
     <height>15</height>
    </rect>
   </property>
   <property name="text">
    <string>TextLabel</string>
   </property>
  </widget>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

展开阅读全文

main()中调用ejb出错

01-22

main()中调用ejb,报rnException in thread "P=994688:O=0:CT" java.lang.ClassCastException: Unable to load class: com.first.interfaces._firstHome_Stubrn at com.ibm.rmi.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:366)rn at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:157)rn at com.first.interfaces.firstUtil.lookupHome(firstUtil.java:25)rn at com.first.interfaces.firstUtil.getHome(firstUtil.java:54)rn at src.testejb.main(testejb.java:36)rn错误rn采用的服务器是webspherern调用的部分代码为:rnHashtable environment=new Hashtable();rnenvironment.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");rnenvironment.put(Context.PROVIDER_URL,"iiop:///");rntry rnrn firstHome fh=firstUtil.getHome(environment);rn first f=fh.create();rn f.test();rnrn其中用的firstUtil.getHome是myeclipse自动生成的方法:rn该方法调用代码如下:rn javax.naming.InitialContext initialContext = new javax.naming.InitialContext(environment);rn try rn Object objRef = initialContext.lookup(jndiName); rn // only narrow if necessaryrn if (java.rmi.Remote.class.isAssignableFrom(narrowTo))rn return javax.rmi.PortableRemoteObject.narrow(objRef, narrowTo);rn else rnreturn objRef;rn finally rn initialContext.close();rn rn rn在 return javax.rmi.PortableRemoteObject.narrow(objRef, narrowTo);rn处出错。rnrnrnejb是采用myeclipse自动生成的框架代码rn代码大体如下:rnpackage com.first.ejb;rnrnimport java.rmi.RemoteException;rnrnimport javax.ejb.EJBException;rnimport javax.ejb.SessionBean;rnimport javax.ejb.SessionContext;rnrn/**rn * XDoclet-based session bean. The class must be declaredrn * public according to the EJB specification.rn *rn * To generate the EJB related files to this EJB:rn * - Add Standard EJB module to XDoclet project propertiesrn * - Customize XDoclet configuration for your appserverrn * - Run XDocletrn *rn * Below are the xdoclet-related tags needed for this EJB.rn * rn * @ejb.bean name="first"rn * display-name="Name for first"rn * description="Description for first"rn * jndi-name="ejb/first"rn * type="Stateless"rn * view-type="remote"rn */rnpublic class firstEJB implements SessionBean rnrn /** The session context */rn private SessionContext context;rnrn public firstEJB() rn super();rn // TODO Auto-generated constructor stubrn rnrn /**rn * Set the associated session context. The container calls this method rn * after the instance creation.rn * rn * The enterprise bean instance should store the reference to the context rn * object in an instance variable.rn * rn * This method is called with no transaction context. rn * rn * @throws EJBException Thrown if method fails due to system-level error.rn */rn public void setSessionContext(SessionContext newContext)rn throws EJBException rn context = newContext;rn rnrn public void ejbRemove() throws EJBException, RemoteException rn // TODO Auto-generated method stubrnrn rnrn public void ejbActivate() throws EJBException, RemoteException rn // TODO Auto-generated method stubrnrn rnrn public void ejbPassivate() throws EJBException, RemoteException rn // TODO Auto-generated method stubrnrn rnrn /**rn * An example business methodrn *rn * @ejb.interface-method view-type = "remote"rn * rn * @throws EJBException Thrown if method fails due to system-level error.rn */rn public void test() throws EJBException rn // rename and start putting your business logic herern System.out.println("test1");rn rnrnrnrn组件接口:rnpackage com.first.interfaces;rn/** * Remote interface for first.rn * @xdoclet-generated at $TODAYrn* @copyright The XDoclet Teamrn * @author XDocletrn * @version $version */rnpublic interface first extends javax.ejb.EJBObjectrn /** * An example business methodrn * @throws EJBException Thrown if method fails due to system-level error. rn */ rnpublic void test( ) throws java.rmi.RemoteException;rnrn主接口:rnpackage com.first.interfaces;rnpublic interface firstHomern extends javax.ejb.EJBHomern public static final String COMP_NAME="java:comp/env/ejb/first";rn public static final String JNDI_NAME="ejb/first";rn public com.first.interfaces.first create() rnthrows javax.ejb.CreateException,java.rmi.RemoteException;rnrnrn打包也是用eclipse自动打包的。rn但这些代码在struts的action中调用是不会出错的,但是是与ejb一起打成的ear包 论坛

main中如何调用面板?

03-20

一下代码中在主函数中调用下面的 private static JPanel createMainPanel()方法?我调用的好像不对!rnimport java.awt.*;rnimport javax.swing.*;rnpublic class JCheckBoxDemol extends JPanelrnrn public static void main(String[] args)rn rn JFrame frame=new JFrame();rn frame.setSize(400,200);rn frame.setTitle("JCheckBoxDemol");rn Container contentPanel=frame.getContentPane();rn contentPanel.add(new JCheckBoxDemol());rn frame.setVisible(true);rn contentPanel.setLayout(new FlowLayout());rn JCheckBoxDemol cb=new JCheckBoxDemol();rn contentPanel.add(cb);rn contentPanel.add(cb.createMainPanel());rn rn [color=#FF0000] private static JPanel createMainPanel()[/color]rn rn JPanel panel=new JPanel();rn JPanel part1=new JPanel();rn part1.add(new JLabel("姓名:"));rn JTextField text=new JTextField();rn text.setColumns(10);rn part1.add(text);rn part1.setVisible(true);rn panel.add(part1);rn rn JPanel part2=new JPanel();rn part2.add(new JLabel("性别:"));rn ButtonGroup group=new ButtonGroup();rn JRadioButton radioButton1=new JRadioButton("男");rn radioButton1.setSelected(true);rn JRadioButton radioButton2=new JRadioButton("女");rn group.add(radioButton1);rn group.add(radioButton2);rn part2.add(radioButton1);rn part2.add(radioButton2);rn part2.setVisible(true);rn panel.add(part2);rn rn JPanel part3=new JPanel();rn part3.setLayout(new GridLayout(2,3));rn JCheckBox musicBox=new JCheckBox("音乐");rn musicBox.setSelected(true);rn part3.add(new JCheckBox("篮球"));rn part3.add(new JCheckBox("上网"));rn part3.add(new JCheckBox("交际"));rn part3.add(new JCheckBox("工作"));rn part3.setVisible(true);rn panel.add(part3);rn rn Panel part4=new Panel();rn part4.add(new JButton("重置"));rn part4.add(new JButton("确认"));rn part4.setVisible(true);rn panel.add(part3);rn panel.setVisible(true);rn return panel;rn rn 论坛

没有更多推荐了,返回首页